What Does Set -E Mean in a Bash Script

What does set - $VARIABLE mean in bash?

This will string-split and glob-expand the contents of $DIR_STACK, putting the first in $1, the second in $2, etc. It's not good practice -- well-written scripts don't rely on string splitting (see the advice at the very top of BashPitfalls, and many of the bugs below are caused by failures to heed that advice).

It's more properly written with --, not -. This is defined by POSIX Utility Syntax Guidelines, entry #10:

The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the - character.

The use of set to change the argument list ($1, $2, etc) is also specified by POSIX, though (again) the standard specifies --, not -:

The remaining arguments [ed: after processing options] shall be assigned in order to the positional parameters. The special parameter # shall be set to reflect the number of positional parameters. All positional parameters shall be unset before any new values are assigned.

The special argument -- immediately following the set command name can be used to delimit the arguments if the first argument begins with + or -, or to prevent inadvertent listing of all shell variables when there are no arguments. The command set -- without argument shall unset all positional parameters and set the special parameter # to zero.

What does set -- “$@” $i mean in Bash?

It's appending the value of $i onto the end of the positional parameters. Not sure why one would want to do it, but it's basically a verbose way of doubling the parameters. It has the same affect as

$ set -- a b c
$ echo "$@"
a b c
$ set -- "$@" "$@"
echo "$@"
a b c a b c

What's set -- $progname $@ means in shell script?

The -- is a bash built-in as well as something a lot of unix commands use to denote the end of command options. So if you have something like:

grep -- -v file

the -v won't be interpreted as a grep option, but a parameter (so you can grep for -v).

The $@ is the list of all the parameters that are passed into the script (which I assume the set command is a part of).

The -- ensures that whatever options passed in as part of the script won't get interpreted as options for set, but as options for the command denoted by the $progname variable.

What does set -t do in bash?

set -t causes bash to immediately exit, because it becomes the one command being read and executed before exiting. it was probably meant to be used for creating a temporary shell. for an example of where it's used see the question @Cyrus posted in comments.

set -T in bash - what does it do?

From help set:

  -T  If set, the DEBUG trap is inherited by shell functions.

So if you use trap to invoke a function on DEBUG (that is, almost before every command in a shell script) and then invoke another shell script, the trapping will occur in that script as well. Without this option, the trap will not exist in the subshell and the script invoked in it will run untrapped.

set -m command in shell script

"Job control" enables features like bg and fg; signal-handling and file-descriptor routing changes intended for human operators who might use them to bring background tasks into the foreground to provide them with input; and the ability to refer to background tasks by job number instead of PID. The script segment you showed doesn't use these features, so the set -m call is presumably pointless.

These features are meant for human users, not scripts; and so in scripts they're off by default. In general, code that attempts to use them in scripts is buggy, and should be replaced with code that operates by PID. As an example, code that runs two scripts in parallel with each other, and then collects the exit status of each when they're finished without needing job control follows:

bash some_bash_file & some_pid=$!
bash some_other_file & some_other_pid=$!
wait "$some_pid"; some_rc=$?
wait "$some_other_pid"; some_other_rc=$?

What is meaning of '#' before a variable in shell script?

In this context, it stands for the the length of the value of that variable:

$ v="hello"
$ echo ${#v}

$ v="bye"
$ echo ${#v}

So what does this command?


It gets the length of the value in $column and substracts the value in $COLON_INDEX using the $(( )) syntax to perform arithmetic operations:

$ column="hello"
$ colon_index=2
$ r=$((${#column}-$colon_index)) # len("hello") - 2 = 5 - 2
$ echo $r

From Arithmetic expression:

(( )) without the leading $ is not a standard sh feature. It comes
from ksh and is only available in ksh, Bash and zsh. $(( ))
substitution is allowed in the POSIX shell. As one would expect, the
result of the arithmetic expression inside the $(( )) is substituted
into the original command. Like for parameter substitution, arithmetic
substitution is subject to word splitting so should be quoted to
prevent it when in list contexts.

What do z${variable} and zfalse in bash script mean?

'z' is there to prevent syntax error in case ${variable} evaluates to nothing. If your user does not provide $1 parameter, ${variable} is empty, and without 'z', the if condition would look something like

if [ != false ] ...

which is syntactically incorrect. With 'z', it becomes:

if [ z != zfalse ] ...

Related Topics

Leave a reply