What does `+option` mean in a bash command? - bash

I have never seen some special bash format before.
special bash command :foo -a -c +b
foo -a -c +b
foo is a bash command ,a and c are foo's options.
For the above command ,what does +b mean here?
To disable an option within a script.
foo command has the b option ,or say,there is a b behavior in foo command,
foo -a -c +b is to invoke ac behavior and disable b behavior.
Why not just run with :
foo -a -c

According to the Advanced Bash-Scripting Guide, for bash options:
-option enables an option that defaults to disabled
+option disables an option that defaults to enabled
So it would make sense that other programs would follow the same method of enabling and disabling options.
A bash example given is:
#!/bin/bash
set -o verbose
# Command echoing on.
command
...
command
set +o verbose
# Command echoing off.
command
# Not echoed.
set -v
# Command echoing on.
command
...
command
set +v
# Command echoing off.
command
exit 0

Related

Is it possible to print all the commands when they are executed in Bash?

From How to echo shell commands as they are executed I've learned how to use set -v to print commands when they are executed. It works well if set -v is added at the beginning of foo.sh or bash -v foo.sh is run. However, if foo.sh invokes bar.sh by bash bar.sh or simply ./bar.sh, the commands in bar.sh are not printed. Is there a global switch that enables all the commands and functions, through all invoked scripts, to be printed when they are executed?
Is there a global switch that enables all the commands and functions, through all invoked scripts, to be printed when they are executed?
Yes, BASH_ENV can be used for that. The manual describes it as follows.
If this variable is set when Bash is invoked to execute a shell script, its value is expanded and used as the name of a startup file to read before executing the script.
So, you can just put set -v in a file and assign its path to BASH_ENV while invoking the first script. For example:
$ cat env.sh
set -v
$
$ cat foo
#!/bin/bash
true
./bar
$
$ cat bar
#!/bin/bash
false
$ BASH_ENV=./env.sh ./foo
#!/bin/bash
true
./bar
#!/bin/bash
false
Alternatively, to do this without having to create an extra file, you can start the script with a fabricated SHELLOPTS value
# too long, and might require adjustments for future versions
env SHELLOPTS=braceexpand:hashall:interactive-comments:verbose ./foo
or, enable verboseness by the set command or by adding -v to the shebang and export SHELLOPTS within the first script.
set -v
# exports all options, not only `verbose'
export SHELLOPTS
# the rest of the script

using set -o in a bash script to set command line editing

I like to use the vi command line editor, however the bash_rc and bash_profile are owned by root. So what is did was create a script that I can run on multiple terminals to set the command line editor to vi. However when I use this script, it says that is sets vi to on, however after running the script, vi is still set to off.
I do not understand.
#!/bin/bash
check_set() {
chckifvi=$(set -o | grep "\bvi\b"| awk '{print $NF}')
}
check_set
echo "VIM command line editing is set to $chckifvi"
if [[ "$chckifvi" == "off" ]] ; then
set -o vi
check_set
echo "VIM Command line editing is set to $chckifvi"
else
echo "VIM Comamnd line editing already set to $chckifvi"
fi
casper#casperfi 1006$ ~/bin/editerSet.sh
VIM command line editing is set to off
VIM Command line editing is set to on
casper#casperfi 1007$ set -o
allexport off
braceexpand on
emacs on
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber off
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
xtrace off
Run . ~/bin/editorSet.sh, not ~/bin/editorSet.sh, to execute the script's commands inside the interactive shell you're already running. (In bash, but not all POSIX shells, you can use source as a synonym for .).
Otherwise, it runs in a new shell which exits when the script does, so the configuration changes do not last past the end of the script's execution.

How can you hide env variables when executing a bash script with -xe?

I have a bash script that I execute with /bin/sh -xe script.sh and I need to capture its output.
The problem is that inside the script I use some ENV variables that I would like to keep them from being displayed in the output.
Is there any way to do this?
Ie. if my script has touch $MY_ENV_VAR and MY_ENV_VAR=ok the output of /bin/sh -xe script.sh will be touch ok.
Is there any way to keep env vars from being replaced in the output of -x? So I would just get + touch $MY_ENV_VAR back.
There is nothing in the POSIX standard which allows this. All you could do is:
set +o xtrace # Switch xtrace off
some hidden code
set -o xtrace # Switch xtrace back on
Alternatively, use the verbose trace instead of xtrace, that's the -v option.
/bin/sh -ve script.sh
By the way, if you are executing with /bin/sh then it is not a bash script, it is a sh script.

How to set case-insensitive completion in a bash instance without Readline support

I have a bash instance without Readline support, i.e. this bash is invoked with the --noediting option. The reason is that this bash instance is used by another program.
The other program wants to know how this bash would complete the command line and for this it issues compgen commands in the bash shell, e.g.
compgen -o default I
This works perfectly in my bash without Readline support. Here an example: Let's say, we are in a directory with three files:
IMG_1234.JPG
about.html
index.html
The command compgen -o default I prints duly
IMG_1234.JPG
However now I want to switch to case-insensitive completion. Normally I issue in the shell
bind 'set completion-ignore-case on'
and in a bash instance with Readline support everything is as expected: compgen -o default I prints
IMG_1234.JPG
index.html
However in my bash instance without Readline support the bind command does nothing and I still get only the IMG_1234.JPG match.
So, my question is: How can I set case-insensitive completion suggestions (when using compgen) in a bash instance which is invoked without Readline support?
Test program:
#!/bin/bash
set -e
echo "### CASE-SENSITIVE"
bash --noediting <<EOF
compgen -o default I
EOF
echo
echo "### CASE-FOLDING"
bash --noediting <<EOF
bind 'set completion-ignore-case on'
compgen -o default I
EOF
Result:
### CASE-SENSITIVE
IMG_1234.JPG
### CASE-FOLDING
bash: line 1: bind: warning: line editing not enabled
IMG_1234.JPG
index.html
Bash version:
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
If your bash version is different, then consider upgrading, as I can't reproduce your issue with 4.3.11. (And you'll probably want to redirect stderr from the bind command; I left it in so you can see it's being executed).

cygwin 1.7.15 handling of "set -e" in shell scripts (error in sub-shell causes parent to exit)

I am using this:
$ uname -a
CYGWIN_NT-6.1 bassoon 1.7.15(0.260/5/3) 2012-05-09 10:25 i686 Cygwin
$ bash --version
GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)
$ cat myexpr.sh
#!/bin/sh
echo "In myexpr, Before expr"
ac_optarg=`expr x--with-gnu-as : 'x[^=]*=\(.*\)'`
echo "ac_optarg=$ac_optarg"
echo "In myexpr, After expr"
$ cat myexpr2.sh
#!/bin/sh
set -e
echo "In myexpr, Before expr"
ac_optarg=`expr x--with-gnu-as : 'x[^=]*=\(.*\)'`
echo "ac_optarg=$ac_optarg"
echo "In myexpr, After expr"
The only difference between the two scripts is that myexpr2.sh uses "set -e"
$ echo $$
2880
$ ./myexpr.sh
In myexpr, Before expr
ac_optarg=
In myexpr, After expr
$ ./myexpr2.sh
In myexpr, Before expr
Expected behavior, so far.
If I do this in the parent shell (PID 2880, above):
$ set -e
$ ./myexpr.sh
The parent shell exits! That is pID 2880 above where I did the "set -e"
This is not the behavior on Linux or cygwin 1.5.12. Is this a bug in cygwin or BASH on cygwin?
This is not a bug, it's a feature of the Bash environment. This happens when you don't have the Bash shell environment variable execfail set, and/or the Shell environment variable errexit.
execfail - (is a BASHOPTS)
If set, a non-interactive shell will not exit if it cannot execute
the file specified as an argument to the exec builtin command.
An interactive shell does not exit if exec fails.
errexit - (is a SHELLOPTS)
Exit immediately if a pipeline (see Pipelines), which may consist of a
single simple command (see Simple Commands), a subshell command enclosed
in parentheses (see Command Grouping), or one of the commands executed as
part of a command list enclosed by braces (see Command Grouping) returns a
non-zero status. The shell does not exit if the command that fails is part
of the command list immediately following a while or until keyword, part
of the test in an if statement, part of any command executed in a && or ||
list except the command following the final && or ||, any command in a
pipeline but the last, or if the command’s return status is being inverted
with !. A trap on ERR, if set, is executed before the shell exits.
This option applies to the shell environment and each subshell environment
separately (see Command Execution Environment), and may cause subshells to
exit before executing all the commands in the subshell.
Different Linux versions have different defaults for these.
You can check which are enabled with:
echo "SHELLOPTS=$SHELLOPTS"
echo "BASHOPTS=$BASHOPTS"
and you can see all of them using:
set -o && echo -e "\n" && shopt -p
So, you need to enable yours with:
shopt -s execfail
If that doesn't work, you may also have to unset (off) the errexit of $SHELLOPTS with:
set -o errexit
For further info, see: The GNU Bash Manual!
PS. "set" is using reverse logic so if you wanna use the 'e' flag you have to use a "+": set +e

Resources