If I assign the output of a command substitution to a local variable, how do I get the exit status of the command?  This is the behaviour of ZSH 5.8:  false; echo $? # output is 1 as expected  foo=$(false); echo $? # output is 1 as expected  local foo=$(false); echo $? # output is 0
123679
Two function definitions, the only difference is that the first combines the local storage keyword with the assignment, while the second separates them:  function foo {     local fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  function bar {     local fn     fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  foo bar   This echoes "0" then "1". I expect it to echo "1" then "1". It seems like the value of $? is the result of the assignment to local, rather than the result of the command substitution.  Why does bash 4.2.46(1)-release behave this way?
113141
I'm working with a program that outputs error messages when something goes wrong, but does not set its exit status accordingly:  the exit status is always 0, indicating success.  I'd like to run this program from a shell script and get a non-zero exit status if it emits any error messages, so the script can tell that the program failed.  The error messages follow a predictable pattern that I can match with grep, and grep sets its exit status based on whether it found a match, so I can pipe the program's output into grep and negate the result with ! to get the exit status I want.  The problem is that if I pipe into grep, I can't see the program's output anymore, because grep consumes it.  I'd like to somehow scan the output for error messages but also display the output normally, since there are other important messages besides the errors.  Unfortunately, grep doesn't have an option to pass-through all of its input, matching and non-matching lines alike.  One approach I've found that mostly works is:  ! my_program | tee /dev/stderr | grep -q "error message"   This feeds the program's output into grep but also copies it to standard error, which isn't redirected, so I can see it.  That's OK when my script's standard output and standard error both go to a terminal (so it doesn't matter which one gets the messages), but it can cause problems if standard out and standard error are redirected to different places; the messages will end up in the wrong place.  Using bash or POSIX shell, and GNU tools, is there a (concise) way to scan a program's standard out and/or standard error streams with something like grep and set exit status accordingly, but also let both streams go to their normal destinations?  (Note that my_program writes its error messages to standard out, not standard error, so a solution that can only scan the standard output stream is OK, though not ideal.  And in case it makes a difference, I'm doing this on CentOS 7.)
20165
I have written a simple script.  When I runsh &lt;myscriptname.sh&gt;, i got the correct output, but when I run ./&lt;myscriptname.sh&gt;, I got an error.  What is difference between when I do sh and ./?
120770
The output of the above command when passed through echo is:  # echo systemctl\ {restart,status}\ sshd\; systemctl restart sshd; systemctl status sshd;   Even if I paste the output to the terminal, the command works. But when I try to directly run the command, I get:  # systemctl\ {restart,status}\ sshd\; bash: systemctl restart sshd;: command not found...   I have two questions..   What exactly is this method of substitution and expansion called? (So that I can research it and learn more about it and how to use it properly).  What did I do wrong here? Why doesn't it work?
27654
I have a script which finds files in the directory specified by user.    #!/bin/bash  # make sure about the correct input if [ -z $1 ] then     echo "Usage: ./script_name.sh    path/to/directory" else     DIR=$1     if [ $DIR = '.' ]     then          echo "Find files in the directory $PWD"     else           echo "Find files in the directory $DIR"     fi      find $DIR -type f -exec basename {} \;  fi   if I input    $ ./script_name.sh .   script gives me correct substitution ./ to $PWD and shows (for example)    $ Find files in the directory /root/scripts     But I can't make a decision how to substitute ../ to the name of the directory  immediately above in the hierarchy.  If I input    $ ./script_name.sh ..     script gives me the output    $ Find files in the directory ..     Does anybody know how to substitute ../ to the actual name of the directory?
14778
I'm trying to use the Bash parameter expansions to modify the output of a command substitution or another parameter expansion. The following nested expansions work quite well in Zsh; but result in a &quot;bad substitution&quot; error in Bash: ${${PWD##*/}//trunk/latest}  or ${$(basename $PWD)//trunk/latest}  the output should be the last folder of the $PWD, replaced by latest when my current directory is trunk so /home/user/trunk should become latest Is there a Bash equivalent allowing to chain expansions without relying on variables or pipes? Or do Bash expansions only allows the input to be a string or a plain variable?
45221
&gt; echo "hi" hi &gt; VAR='echo "hi"' &gt; $VAR "hi"   Why is the output of the above commands different?  A similar thing occurs with single quotes:  &gt; VAR="echo 'hi'" &gt; $VAR &gt; 'hi'
48113
Consider this snippet:  stop () {     echo "${1}" 1&gt;&amp;2     exit 1 }  func () {     if false; then         echo "foo"     else         stop "something went wrong"     fi }   Normally when func is called it will cause the script to terminate, which is the intended behaviour. However, if it's executed in a sub-shell, such as in  result=`func`   it will not exit the script. This means the calling code has to check the exit status of the function every time. Is there a way to avoid this? Is this what set -e is for?
206
From the Advanced Bash-Scripting Guide, to get the last command-line argument:     Or: lastarg=${!#}    This is an   indirect reference to the $# variable.    Note that lastarg=${!$#} doesn't work.
54043
This is zsh -f:  Fereidoons-MacBook-Pro% local a=$(jaja) &amp;&amp; echo bad zsh: command not found: jaja bad Fereidoons-MacBook-Pro% a=$(jaja) &amp;&amp; echo bad zsh: command not found: jaja Fereidoons-MacBook-Pro%   Why is local messing up error handling?
30221
Suppose I want a bash command to do something extra.  As a simple example, imagine I just want it to echo "123" before running.  One simply way to do this would be to alias the command.  Since we still need the original, we can just refer to it by its exact path, which we can find using which.  For example:  $ which rm /bin/rm $ echo "alias rm='echo 123 &amp;&amp; /bin/rm'" &gt;&gt; .bashrc   This was easy because I was able to look up the path to rm using which.  However, I am trying to do this with exit, and which doesn't seem to know anything about it.  $ which exit $ echo $? 1   The command did not output a path, and in fact it returned a non-zero exit code, which which does when a command is not in $PATH.  I thought maybe it's a function, but apparently that's not the case either:  $ typeset -F | grep exit $ echo $? 1   So the exit command is not defined anywhere as a function or as a command in $PATH, and yet, when I type exit, it closes the terminal.  So it clearly is defined somewhere but I can't figure out where.  Where is it defined, and how can I call to it explicitly?
2926
How should I write the command variable to get the expected behavior?  #!/bin/bash  function f ( ) {     echo "$2" } command="f --option=\"One Two Three\" --another_option=\"Four Five Six\"" $command  f --option="One Two Three" --another_option="Four Five Six"   First calling is wrong, second - right  $&gt; ./test.sh  Two --another_option=Four Five Six
109280
Consider the two shell samples  $ ls myDoc.html SomeDirectory someDoc.txt   and  $ echo $(ls) myDoc.html SomeDirectory someDoc.txt   The first executes ls which, as I understand, appends the contents of the current working directory to the stdout file (which is what the terminal displays).  Is this correct?  The second takes the value of the ls command (which means is the contents of the current working directory) and prints it to the stdout file. Is this correct?  Why do the two commands give different outputs?
32522
${!FOO} performs a double substitution in bash, meaning it takes the (string) value of FOO and uses it as a variable name. zsh doesn’t support this feature.  Is there a way to make this work the same in bash and zsh?  Background:  I’ve got a list of environment variables, like  PATH MAIL EDITOR   and want to first print the variable names and afterwards their values.  This works in bash but not zsh:    for VAR in LIST do         echo $VAR         echo ${!VAR} done   It should be somehow possible “the old way” with eval, but I can’t get it to work:  for VAR in LIST do         echo $VAR         echo `eval \$$VAR` done   I’m never going to understand why I can’t simply do arbitrary deep substitutions like ${${VAR}} or even ${${${VAR}}} if need be, so an explanation for that would be nice, too.
83139
I wish to use shell to invoke a script on a remote server. I would like to capture the output of that script (its logging messages) and the exit code it returns.   If I do this:  ssh user@server /usr/local/scripts/test_ping.sh echo "$?"   I get the exit code but can't capture the remote logging messages .  If I do this:  local RESULTS=$(ssh user@server /usr/local/scripts/test_ping.sh) echo "$?"  LOG "${RESULTS}";   I get to log my output using my LOG function but can't seem to get a correct exit code, I assume the code I get is the code from the varianble assignment.   I would like to continue to use my LOG function to capture all output as it formats and sends things to a file, syslog, and the screen for me.   How can I capture results in a var AND get the correct exit code from the remote script?
91295
Why doesn't echo $1 print $1 in this simple bash script?    #!/bin/bash # function.sh print_something () { echo $1 } print_something  $ ./function.sh 123  -&gt; why doesn't it print '123' as a result?
77306
When run a command, if the command is not present, then some information regarding the failure of the command is displayed.  I am trying to take the information about that failed command as input to my script, which has to run automatically whenever the command fails.  Whenever the command fails the $? value would be 127. I have to catch this failed event and run my commands there.