Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
1
answers
70
views
Understanding `bash` options -Ee
In a larger script I have ```bash trap -p echo "settings: [$-], command: [$*]" "$@" echo "return code $?" ``` and I get the output ``` trap -- '_shunit_cleanup EXIT' EXIT trap -- '_shunit_cleanup INT' SIGINT trap -- '_shunit_cleanup TERM' SIGTERM settings: [ehuBET], command: [false VAR2 VAR1] return...
In a larger script I have
trap -p
echo "settings: [$-], command: [$*]"
"$@"
echo "return code $?"
and I get the output
trap -- '_shunit_cleanup EXIT' EXIT
trap -- '_shunit_cleanup INT' SIGINT
trap -- '_shunit_cleanup TERM' SIGTERM
settings: [ehuBET], command: [false VAR2 VAR1]
return code 1
We see that the command false
is run and we see that it exits with code 1
. But we also have set -e
from which I would expect that we should never see the line return code 1
, because as soon as false
exists with 1
the whole script should stop. Afaict the traps are not relevant.
I tried to reproduce the situation with a minimal example, but in all smaller tests, the immediate exit due to -e
does happen.
Which setting or options should I look at to explain the above?
**More background and findings:**
The problematic code is called as part of a shunit2
test from within a testing function. If I call that same testing function directly, without invoking it through shunit2
, the problem is gone.
But to confirm: my confusion lies mostly in the facts which can be seen from the output above alone: set -e
seems to be on, false
is called but instead of exiting right away, the next line of code (echo
) is executed anyway.
Harald
(1030 rep)
Apr 27, 2025, 07:22 AM
• Last activity: Jun 12, 2025, 02:44 PM
4
votes
1
answers
134
views
Bash extglob with ignored pattern
Suppose I have these files: ``` foo/bar/baz/test.js foo/bar/baz/test.min.js ``` If I run: ```sh shopt -s globstar shopt -s extglob echo foo/bar/**/*!(.min).js ``` ...that will nonetheless match the `test.min.js` file. How do I ignore it?
Suppose I have these files:
foo/bar/baz/test.js
foo/bar/baz/test.min.js
If I run:
shopt -s globstar
shopt -s extglob
echo foo/bar/**/*!(.min).js
...that will nonetheless match the test.min.js
file.
How do I ignore it?
lonix
(1965 rep)
Dec 13, 2024, 11:01 AM
• Last activity: Jan 6, 2025, 01:58 PM
0
votes
1
answers
140
views
Using shopt extdebug inside .bashenv
I'm using Ubuntu within Windows Subsystem for Linux. With the approach suggested by this [answer](https://unix.stackexchange.com/a/771542/305710) I can run code when a new non-interactive shell is started: ```shell $ cat ~/.bashenv if [[ $- != *i* ]]; then echo foo fi $ export BASH_ENV=~/.bashenv $...
I'm using Ubuntu within Windows Subsystem for Linux.
With the approach suggested by this [answer](https://unix.stackexchange.com/a/771542/305710) I can run code when a new non-interactive shell is started:
$ cat ~/.bashenv
if [[ $- != *i* ]]; then
echo foo
fi
$ export BASH_ENV=~/.bashenv
$ bash -c 'echo bar'
foo
bar
Now I am looking to setup a trap function with shopt extdebug enabled.
I tried something like this:
$ cat ~/.bashenv
#!/bin/bash
say_foo() {
echo "foo"
}
set -T
trap 'say_foo' DEBUG
shopt -s extdebug
$ export BASH_ENV=~/.bashenv
$ bash -c 'echo bar'
foo
bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
bash: warning: cannot start debugger; debugging mode disabled
foo
bar
Is there any way to fix this?
---
A shorter way of reproducing the same issue:
$ bash --debugger -c 'echo hello'
bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
bash: warning: cannot start debugger; debugging mode disabled
hello
(Running an in-line bash
script with --debugger
is the same as setting extdebug
in a file read via $BASH_ENV
.)
Foo
(242 rep)
Mar 14, 2024, 04:35 PM
• Last activity: Mar 19, 2024, 11:02 PM
1
votes
1
answers
228
views
Expand a list of files including globs, which are defined in a file
I'm using bash. Suppose I have a file named `filelist`, which contains a list of files: ``` stuff/**/*.csv # to keep it simple (without loops), assume just one entry #*.txt #foo.md #bar.bin ``` And I want to expand that: ```sh ls $(cat filelist) ``` I get: > ls: cannot access 'stuff/**/*.csv': No su...
I'm using bash. Suppose I have a file named
filelist
, which contains a list of files:
stuff/**/*.csv # to keep it simple (without loops), assume just one entry
#*.txt
#foo.md
#bar.bin
And I want to expand that:
ls $(cat filelist)
I get:
> ls: cannot access 'stuff/**/*.csv': No such file or directory
What I want is to expand that into a list of files which I can pipe to my main app for processing. How can I do that?
lonix
(1965 rep)
Dec 30, 2023, 06:28 AM
• Last activity: Dec 30, 2023, 02:41 PM
2
votes
0
answers
80
views
Pattern list in bash extglob containing a / and a |
Here is a transcript of commands (and their output) that explains my problem: ``` /tmp/example $ shopt -s cdspell on checkwinsize on cmdhist on complete_fullquote on direxpand on dirspell on expand_aliases on extglob on extquote on failglob on force_fignore on globstar on histappend on interactive_c...
Here is a transcript of commands (and their output) that explains my problem:
/tmp/example $ shopt -s
cdspell on
checkwinsize on
cmdhist on
complete_fullquote on
direxpand on
dirspell on
expand_aliases on
extglob on
extquote on
failglob on
force_fignore on
globstar on
histappend on
interactive_comments on
login_shell on
nocaseglob on
nullglob on
progcomp on
promptvars on
sourcepath on
/tmp/example $ ls -R
.:
prefix_bar prefix_foo
./prefix_bar:
test.c
./prefix_foo:
baz test.c
./prefix_foo/baz:
test.c
/tmp/example $ ls prefix_@(foo/baz|bar)/test.c
prefix_bar/test.c
/tmp/example $ ls @(prefix_foo/baz|prefix_bar)/test.c
prefix_bar/test.c
Question: Why does bash ignore the foo/baz
or the pattern_foo/baz
portion of the pattern-list?
For the record, I looked around before posting this question and the question closest to what I am about to ask is https://unix.stackexchange.com/questions/548745/bash-extglob-should-the-order-of-patterns-in-a-pattern-list-matter (and it does not answer my question).
Happy Green Kid Naps
(121 rep)
Oct 6, 2023, 11:10 PM
114
votes
7
answers
29844
views
Set and Shopt - Why Two?
`set` and `shopt` are both shell builtins that control various options. I often forget which options are set by which command, and which option sets/unsets (`set -o/+o`, `shopt -s/-u`). Why are there two different commands that seemingly do the same thing (and have different arguments to do so)? Is...
set
and shopt
are both shell builtins that control various options. I often forget which options are set by which command, and which option sets/unsets (set -o/+o
, shopt -s/-u
). Why are there two different commands that seemingly do the same thing (and have different arguments to do so)? Is there any easy way/mnemonic to remember which options go with which command?
Kevin
(41635 rep)
Feb 22, 2012, 04:17 PM
• Last activity: Jun 2, 2023, 07:40 AM
7
votes
5
answers
4674
views
Check for bash options
I have a function and would like to use the `pipefail` option inside. But I don't want to simply `set -o pipefail`, because I fear that another part of the script may not expect `pipefail` to be set. Of course I could do `set +o pipefail` afterwards, but in case `pipefail` was set outside of my func...
I have a function and would like to use the
pipefail
option inside. But I don't want to simply set -o pipefail
, because I fear that another part of the script may not expect pipefail
to be set. Of course I could do set +o pipefail
afterwards, but in case pipefail
was set outside of my function, this may introduce unexpected behavior as well.
Of course I could use the exit code of false | true
as a measure, if pipefail
is set, but this seems a little bit dirty.
Is there a more general (and maybe canonical?) way to check the set bash options?
red_trumpet
(345 rep)
Nov 22, 2017, 08:26 AM
• Last activity: Jun 21, 2022, 07:48 PM
59
votes
5
answers
31072
views
How can I list Bash's options for the current shell?
The Bash interpreter itself has options. For example, those mentioned on lines 22-23 of [Bash's man page](https://man7.org/linux/man-pages/man1/bash.1.html#OPTIONS): > OPTIONS > All of the single-character shell options documented in the description of the set builtin command can be used as options...
The Bash interpreter itself has options.
For example, those mentioned on lines 22-23 of [Bash's man page](https://man7.org/linux/man-pages/man1/bash.1.html#OPTIONS) :
> OPTIONS
> All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked. In addition, bash interprets the following options when it is invoked:
-c ...
-i ...
-l ...
-r ...
I've used a few search patterns in Bash's man page like:
*
/^\s*set
* /list
Is it possible to print a list of these settings that are applied to the current shell?
the_velour_fog
(12760 rep)
Jun 17, 2015, 07:04 AM
• Last activity: May 20, 2022, 05:03 PM
10
votes
1
answers
764
views
Setting bash options in a compound command
I have found that setting the `extglob` shell option only within a compound compound results in failure of subsequent extended globs. Are shell options required to be set outside of compound commands? I did not see an indication of such a requirement in the Bash man pages. As an example, the followi...
I have found that setting the
extglob
shell option only within a compound compound results in failure of subsequent extended globs. Are shell options required to be set outside of compound commands? I did not see an indication of such a requirement in the Bash man pages.
As an example, the following script works fine (prints a.0 a.1
):
#!/bin/bash
touch a.0 a.1 \
a.b.0 a.b.1
shopt -s extglob
ls "a."!(b*)
However, if the last two lines are executed as a compound command, the script fails with the following error:
syntax error near unexpected token `('
` ls "a."!(b*)'
This was tested using Bash versions from 4.2 to 4.4 and with a variety of compound commands :
(1) conditional -- if
#!/bin/bash
touch a.0 a.1 \
a.b.0 a.b.1
if true; then
shopt -s extglob
ls "a."!(b*)
fi
(2) braces -- { }
#!/bin/bash
touch a.0 a.1 \
a.b.0 a.b.1
{
shopt -s extglob
ls "a."!(b*)
}
(3) subshell -- ( )
:
#!/bin/bash
touch a.0 a.1 \
a.b.0 a.b.1
(
shopt -s extglob
ls "a."!(b*)
)
In all cases, if the shopt
is moved outside the compound command, the script succeeds.
user001
(3808 rep)
Jun 2, 2019, 10:13 PM
• Last activity: Nov 16, 2021, 11:37 AM
5
votes
1
answers
1872
views
Are there any caveats in using shopt -s autocd?
I have recently discovered the feature `shopt -s autocd`: autocd If set, a command name that is the name of a directory is executed as if it were the argument to the cd command. This op‐ tion is only used by interactive shells. At first glance it seems helpful but I am not an expert Bash user and I...
I have recently discovered the feature
shopt -s autocd
:
autocd If set, a command name that is the name
of a directory is executed as if it were
the argument to the cd command. This op‐
tion is only used by interactive shells.
At first glance it seems helpful but I am not an expert Bash user and I wonder if it may be a mistake to use it.
Are there any potential dangers to setting shopt -s autocd
? I am especially interested in terms of scripting and conflicts with other applications or configurations.
mgarort
(465 rep)
Apr 26, 2021, 01:24 PM
• Last activity: Apr 26, 2021, 04:17 PM
1
votes
0
answers
205
views
Usage of gnu_errfmt option in bash
I readed [GNU/Bash document][1] but i can't understand or find any realworld usage example. **gnu_errfmt** > If set, shell error messages are written in the standard GNU error > message format. **When i try:** ``` └─$ shopt -u gnu_errfmt └─$ no_command -bash: no_command: command not found └─$ shopt...
I readed GNU/Bash document but i can't understand or find any realworld usage example.
**gnu_errfmt**
> If set, shell error messages are written in the standard GNU error
> message format.
**When i try:**
└─$ shopt -u gnu_errfmt
└─$ no_command
-bash: no_command: command not found
└─$ shopt -s gnu_errfmt
└─$ no_command
-bash: no_command: command not found
May you please give me real usage purpose and example.
testter
(1510 rep)
Mar 31, 2021, 02:28 PM
0
votes
1
answers
921
views
What does shopt histreedit do?
`shopt histreedit` is supposed to allow the user to re-edit a failed history substitution. How can a history substitution fail? How is `histreedit` different from `histverify`?
shopt histreedit
is supposed to allow the user to re-edit a failed history substitution.
How can a history substitution fail?
How is histreedit
different from histverify
?
EmmaV
(4359 rep)
Mar 27, 2021, 05:34 PM
• Last activity: Mar 27, 2021, 05:58 PM
7
votes
1
answers
390
views
Should the option "--rcfile /dev/null" have the same effect as "--norc" when invoking bash?
To avoid the XY problem scenario, I'll explain why I'm asking this question. I remember I had `shopt -s extglob` set in my `~/.bashrc` file, because things like `@(pattern-list)` do work. However, I've just looked into `~/.bashrc` and that option is not set. So I decided to see where it comes from,...
To avoid the XY problem scenario, I'll explain why I'm asking this question.
I remember I had
shopt -s extglob
set in my ~/.bashrc
file, because things like @(pattern-list)
do work.
However, I've just looked into ~/.bashrc
and that option is not set.
So I decided to see where it comes from, and discovered that
- if I launch another shell via bash --norc
and then run shopt
I see the line extglob off
,
- whereas, if I launch it via bash --rcfile /dev/null
, then shopt
shows that extglob on
,
which doesn't really help me in find out which file is extglob
is set in.
Based on a comment, PS4=' $BASH_SOURCE:$LINENO: ' bash -lixc true |& grep extglob
gives
/usr/share/bash-completion/bash_completion:45: shopt -s extglob progcomp
/usr/share/bash-completion/bash_completion:45: shopt -s extglob progcomp
Enlico
(2258 rep)
Mar 11, 2021, 08:18 AM
• Last activity: Mar 11, 2021, 03:55 PM
-2
votes
2
answers
95
views
mv cdable_vars - where has my .bashrc gone?
I thought it would be clever to have an alias to my `dotfiles` repo, where I keep my dotfiles such as `.bashrc`. Following a [recommendation](https://stackoverflow.com/a/39839346/4556479), I added this to my `.bashrc`: ```bash shopt -s cdable_vars export dotfiles=$HOME/dotfiles/ ``` Then I wanted to...
I thought it would be clever to have an alias to my
dotfiles
repo, where I keep my dotfiles such as .bashrc
.
Following a [recommendation](https://stackoverflow.com/a/39839346/4556479) , I added this to my .bashrc
:
shopt -s cdable_vars
export dotfiles=$HOME/dotfiles/
Then I wanted to test, by moving my bashrc to dotfiles with:
mv .bashrc dotfiles
However, the bashrc never appeared in $HOME/dotfiles/
- it's also not in my home folder anymore.
Anyone knows where it could be gone?
Alex
(137 rep)
Dec 13, 2020, 08:21 AM
• Last activity: Dec 13, 2020, 10:48 AM
1
votes
0
answers
382
views
is it enough to set "shopt -s nocaseglob" once / script?
Is it enough to set `shopt -s nocaseglob` once in a bash-script, or do I need to set it before every function in the script. Do I need to turn it on again after the script is completed or is this only a temporary setting for just that terminal?
Is it enough to set
shopt -s nocaseglob
once in a bash-script, or do I need to set it before every function in the script.
Do I need to turn it on again after the script is completed or is this only a temporary setting for just that terminal?
JoBe
(417 rep)
Aug 10, 2020, 05:36 PM
• Last activity: Aug 10, 2020, 07:18 PM
1
votes
1
answers
485
views
bash overrides the history even with histappend set to on
I'm trying to write the bash history to a file (`...| 1 |...`), then change one character (`...| 2 |...`) in `HISTTIMEFORMAT` environment variable run couple commands and being able to see commands from the first run as well as commands from the second run. Questions: 1. Why when I change the value...
I'm trying to write the bash history to a file (
...| 1 |...
), then change one character (...| 2 |...
) in HISTTIMEFORMAT
environment variable run couple commands and being able to see commands from the first run as well as commands from the second run.
Questions:
1. Why when I change the value it changes all the values before that in the same "column"?
2. Why shopt -s histappend
doesn't make a difference?
OS:
[root@test ~]# cat /etc/*-release
CentOS release 6.10 (Final)
[root@test ~]# export HISTTIMEFORMAT='%a %h %d | %T | 1 |'; shopt -s histappend; echo 1; history 3
872 Wed Jun 17 | 22:14:48 | 1 |export HISTTIMEFORMAT='%a %h %d | %T | 1 |'; shopt -s histappend; echo 1; history 3
873 Wed Jun 17 | 22:14:50 | 1 |export HISTTIMEFORMAT='%a %h %d | %T | 1 |'; shopt -s histappend; echo 1; history 3
874 Wed Jun 17 | 22:14:52 | 1 |export HISTTIMEFORMAT='%a %h %d | %T | 1 |'; shopt -s histappend; echo 1; history 3
[root@test ~]# export HISTTIMEFORMAT='%a %h %d | %T | 2 |'; shopt -s histappend; echo 2; history 3
873 Wed Jun 17 | 22:14:50 | 2 |export HISTTIMEFORMAT='%a %h %d | %T | 1 |'; shopt -s histappend; echo 1; history 3
874 Wed Jun 17 | 22:14:52 | 2 |export HISTTIMEFORMAT='%a %h %d | %T | 1 |'; shopt -s histappend; echo 1; history 3
875 Wed Jun 17 | 22:15:02 | 2 |export HISTTIMEFORMAT='%a %h %d | %T | 2 |'; shopt -s histappend; echo 2; history 3
Vor
(113 rep)
Jun 17, 2020, 11:17 PM
• Last activity: Jun 18, 2020, 12:34 AM
2
votes
1
answers
1145
views
Aliases don't work in interactive zsh
I have a curious problem where my aliases are listed when I type `alias`, but they are not executable. Here, I try to alias `d` to `date`: ``` % which alias alias: shell built-in command % alias d=/usr/bin/date ``` But it doesn't run: ``` % d zsh: command not found: d % /usr/bin/date Sun 19 Apr 2020...
I have a curious problem where my aliases are listed when I type
alias
, but they are not executable.
Here, I try to alias d
to date
:
% which alias
alias: shell built-in command
% alias d=/usr/bin/date
But it doesn't run:
% d
zsh: command not found: d
% /usr/bin/date
Sun 19 Apr 2020 20:30:06 +07
Is there some obscure option that I'm missing?
Tom Hale
(32892 rep)
Apr 19, 2020, 01:42 PM
• Last activity: Apr 20, 2020, 06:07 PM
2
votes
1
answers
1887
views
Extended globbing inside script - what am I doing wrong?
so I'm trying to select a range of files using an interactive script. The end goal is to use the `read` command but for demonstration here I assigned the `glob` variable manually #!/bin/bash shopt -s extglob # read -rp "Please enter a globbing string:"$'\n' glob # This will give me an error (See bel...
so I'm trying to select a range of files using an interactive script.
The end goal is to use the
read
command but for demonstration here I assigned the glob
variable manually
#!/bin/bash
shopt -s extglob
# read -rp "Please enter a globbing string:"$'\n' glob
# This will give me an error (See below)
glob=*2020_04_03_{06..18}.jpg
/bin/ls -la /mnt/drive1/images/*/*/${glob}
# While this will return the desired files
/bin/ls -la /mnt/drive1/images/*/*/*2020_04_03_{06..18}.jpg
The error is as follows:
Error /bin/ls: cannot access "/mnt/drive1/images/*/*/*2020_04_03_{06..18}.jpg": No such file or directory
So what am I missing here in either assigning the glob
variable or appending the glob
variable to my path?
**Solution**:
I found a solution but I'm not quite sure why but
bash <
Guidable
(29 rep)
Apr 5, 2020, 07:00 PM
• Last activity: Apr 9, 2020, 03:45 PM
1
votes
1
answers
136
views
Which bash shell options are used by the parser?
Restricting our focus exclusively to `bash`, in [this answer][1] on Stack Overflow it is reported the following. > `extglob` is a flag used *by the parser*. Functions, compound commands, &c. are parsed in entirety ahead of execution. Thus, `extglob` must be set before that content is parsed; setting...
Restricting our focus exclusively to
bash
, in this answer on Stack Overflow it is reported the following.
> extglob
is a flag used *by the parser*. Functions, compound commands, &c. are parsed in entirety ahead of execution. Thus, extglob
must be set before that content is parsed; setting it at execution time but after parse time does not have any effect for previously-parsed content.
>
> This is also why you can't run shopt -s extglob; ls !(*.txt)
as a one-liner (when extglob
is previously unset), but must have a newline between the two commands.
However, this is not true for other shell options. Consider e.g. the following.
$ ls -a
. .. .hiddenFile file1 file2 file3
$ shopt dotglob
dotglob off
$ echo *
file1 file2 file3
$ shopt -s dotglob; echo *
.hiddenFile file1 file2 file3
**Is it documented somewhere which shell options are used by the parser like extglob
and therefore cannot be enabled within a group command that uses them?**
In the shopt
page in the bash
manual nothing about the behaviour above seems to be mentioned.
Axel Krypton
(336 rep)
Mar 18, 2020, 02:07 PM
• Last activity: Mar 18, 2020, 03:05 PM
0
votes
1
answers
562
views
Does setting the bash shell option 'checkhash' actually improve the performance of bash commands?
__Premise:__ In the process of modifying `.bashrc`, and going through the [bash manual for shopt online](https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html) page for the different shell options that can be handled by `shopt`, I came across the option `checkhash`, which accordi...
__Premise:__ In the process of modifying
1. Is this option useful, i.e, does it increase the performance of the bash commands? 2. If, yes, why is the __default value__ set to
.bashrc
, and going through the [bash manual for shopt online](https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html) page for the different shell options that can be handled by shopt
, I came across the option checkhash
, which according to the description:checkhash
If this is set, Bash checks that a command found in the hash table exists
before trying to execute it.
If a hashed command no longer exists, a normal path search is performed.
__Questions:__1. Is this option useful, i.e, does it increase the performance of the bash commands? 2. If, yes, why is the __default value__ set to
off
?
3. If, no, why does the option exist in the first place, is it something to do with older hardware?
RG7
(115 rep)
Feb 21, 2020, 06:11 AM
• Last activity: Feb 21, 2020, 06:28 AM
Showing page 1 of 20 total questions