How can I get unmodified output from $() in bash?  I need to have bash return the results of command substitution (via $(command) or `command`), but bash treats the left square bracket as the test builtin and evaluates the text.   &gt; echo "This shows an IP in brackets [127.0.0.1] when not in the command substitution context." This shows an IP in brackets [127.0.0.1] when not in the command substitution context.  &gt; echo $(echo "This should show an IP in brackets [127.0.0.1] but instead evaluates to '1'.") This should show an IP in brackets 1 but instead evaluates to '1'.  &gt; echo $(echo "The subtle addition of a space causes [127.0.0.1 ] to behave differently.") The subtle addition of a space causes [127.0.0.1 ] to behave differently.   Background  Please note that the use of echo above to provide a minimal example is just that; my ultimate use will be including output from ping or tracert to show the IP of a host as seen by that host (i.e. respecting any hosts entries). In this usage, I have little control over the string being output by the command.  &gt; grep -E "^[^#].*stackoverflow" /c/Windows/System32/drivers/etc/hosts 127.0.0.1 stackoverflow.com  &gt; tracert -h 1 -w 1 stackoverflow.com | awk '/Tracing/' Tracing route to stackoverflow.com [127.0.0.1]  &gt; ping -n 1 -w 1 stackoverflow.com | awk '/Pinging/' Pinging stackoverflow.com [127.0.0.1] with 32 bytes of data:  # Using dig or nslookup would show a real IP rather than the forced IP from the hosts entry. &gt; dig @ns1 +nocmd stackoverflow.com +noall +answer stackoverflow.com.      118     IN      A       151.101.129.69 stackoverflow.com.      118     IN      A       151.101.193.69 stackoverflow.com.      118     IN      A       151.101.1.69 stackoverflow.com.      118     IN      A       151.101.65.69  &gt; echo $(ping -n 1 -w 1 stackoverflow.com | awk '/Pinging/') Pinging stackoverflow.com 1 with 32 bytes of data:  &gt; echo $(ping -n 1 -w 1 stackoverflow.com | awk '/Pinging/ {print $3}') 1  &gt; ping -n 1 -w 1 stackoverflow.com | awk '/Pinging/ {print $3}' [127.0.0.1]
123931
It's high time to solve this conundrum that's been bothering me for years...  I've been meeting this from time to time and thought this is the way to go:  $(comm "$(arg)")   And thought my view was strongly supported by experience. But I'm not so sure anymore.  can't make up its mind too. It's both:  "$(dirname $0)"/stop.bash            ^-- SC2086: Double quote to prevent globbing and word splitting.   And:  $(dirname "$0")/stop.bash ^-- SC2046: Quote this to prevent word splitting.   What's the logic behind?   (It's Shellcheck version 0.4.4, btw.)
76187
How can I test whether a command's output is an empty string while not   interfering with buffering?  Example:  command | ??? if [ $? = 0 ] &amp;&amp; [ -n "$output_from_command"]; then    ...   I want the output from the code above to be the same as when running command.
15751
I saw the following syntax in a bash script:   &gt;$(command) &amp;   I know that the ampersand &amp; at the end makes a command run in the background, but I have never seen it in combination with the dollar sign plus the brackets around the command.  What does it do?
47782
I am trying out the command  $ b=5; echo `$b`; -bash: 5: command not found   but it does not print 5 as it is supposed to. What am I missing here?   seems to say that ` evaluates the commands within and replaces them with the output.
71966
I have a bash application that is producing some result, and I'd like to echo the result to either stdout or to a user chosen file. Because I also echo other interactive messages going to the screen, requiring the user to explicitly use the &gt; redirection when he wants to echo the result to a file is not an option (*), as those messages would also appear in the file.  Right now I have a solution, but it's ugly.  if [ -z $outfile ] then     echo "$outbuf"    # Write output buffer to the screen (stdout) else     echo "$outbuf" &gt; $outfile  # Write output buffer to file fi   I tried to have the variable $outfile to be equal to stdout, to &amp;1 and perhaps something else but it would just write to file having that name and not actually to stdout. Is there a more elegant solution?  (*) I could cheat and use stderr for that purpose, but I think it's also quite ugly, isn't it?
69637
i am writing a script in bash and I would like to give the two numbers in the brackets, x and y to be precise, as variables.  I wrote the script this way:  echo "x?" read x echo "y" read y  for i in {$x..$y}   but it does not work.  How could I set it up to make it function?  Thanks a lot in advance.
43099
On the paragraph explaining arithmetic expansion,  uncovers 2 different ways of evaluating an expression, the first one uses $(( EXPRESSION )) and the second one uses $[ EXPRESSION ]. The two ways seem pretty similar as the only difference I have found is:     $[ EXPRESSION ] will only calculate the result of EXPRESSION, and do no tests:   Yet, I am intrigued because the same document recommends using $[ EXPRESSION ] rather than $(( EXPRESSION )).     Wherever possible, Bash users should try to use the syntax with square brackets:   Why would you want that if less tests are being done?
106954
When a script runs, commands in it may output some text to stdout/stderr. Bash itself may also output some text.  But if a few scripts are running at the same time, it is hard to identify where does an error come from.  So is it possible to insert a prefix to all output of the script? Something like:  #!/bin/bash prefix 'PREFIX' &amp;2 echo "wrong!" &gt;&amp;2   Then:  $ ./script.sh PREFIXwrong!
14684
I am trying to perform a mathematical operation with sed, but it continues to treat my variables as strings. The input is of this kind:  $ echo 12 | sed 's/[0-9]*/&amp;+3/' $ 12+3   I'd like to have 15 as output. I need to do the operation and replace its mathematical result in only one passage, because I am running the program as a Python daemon, and I want to avoid passages like redirecting stdout on files, open those files, perform operations, extract the result, do the replacement. To me, sed seems the best to perform all in one line.  I've tried to cast both input and output in various ways like  $ echo 12 | sed 's/[0-9]*/int(&amp;+3)/' $ echo 12 | sed 's/[0-9]*/\int(&amp;+3)/' $ echo 12 | sed 's/[0-9]*/\int(&amp;+3)/'   but the result was always a printing of the second field.
110271
I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string.  This works perfectly.  echo $(ipcs | grep Shared | awk '{print $2}')   When I put it in an if statement I get some problems.  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" -eq "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" = "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" == "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false   I tried ==, =, and -eq because I wasn't sure which one to use.
26600
On the bash command-line, ↑ gives me the previous command. On the command-lines in ipython or matlab, when I type a few characters, ↑ gives me the previously entered command starting with those characters. How can I enable exactly this behaviour in bash? I am aware of more advanced ways of searching through the command-line history, but sometimes a simple way is more convenient.
778
Usually, $0 in a script is set to the name of the script, or to whatever it was invoked as (including the path). However, if I use bash with the -c option, $0 is set to the first of the arguments passed after the command string:  bash -c 'echo $0' foo bar  # foo    In effect, it seems like positional parameters have been shifted, but including $0. However shift in the command string doesn't affect $0 (as normal):  bash -c 'echo $0; shift; echo $0' foo bar # foo # foo   Why this apparently odd behaviour for command strings? Note that I am looking for the reason, the rationale, behind implementing such odd behaviour.     One could speculate that such a command string wouldn't need the $0 parameter as usually defined, so for economy it is also used for normal arguments. However, in that case the behaviour of shift is odd. Another possibility is that $0 is used to define the behaviour of programs (a la bash called as sh or vim called as vi), but that cannot be, since $0 here is only seen in the command string and not by programs called within it. I cannot think of any other uses for $0, so I am at a loss to explain this.
51024
When I am running two while loop and trying to concatenating two variable strings (from two loops), the final output (string) is weird. I have tried different way to concatenating but did not get the answer: Below is the code: [I need vfile="var1_Amon_My_model1*.nc"]  #!/bin/bash jcount=1 extn="*.nc" while read line do mname=$line echo " Working on model - " $mname echo " and model number = "$jcount while read line do vname=$line vfile="${vname}""_Anom_""${mname}""${extn}" echo $vfile done&lt;varlists.txt echo "******************************************" jcount=$((jcount+1)) done&lt;model_test1.txt   In varlists.txt  var1 var2   In model_test1.txt  My_model1 My_model2   After executing the script, it is showing   Working on model -  My_model1  and model number = 1 *.ncm_My_model1 *.ncm_My_model1 ******************************************
393
For bash script, I can use "$@" to access arguments. What's the equivalent when I use an alias?
4896
How can I get the pid of a subshell?  For example:  $ echo $$ 16808   This doesn't work, because the original shell expands $$:  $ ( echo $$ ) 16808   Why does single quoting not work? After the original shell removes the single quote, does the subshell not expand $$ in itself?  $ ( echo '$$' ) $$   Why does eval not work either? Is eval run by the subshell?  Why does it give me the original shell's PID?  $ ( eval echo '$$' ) 16808   Thanks.
23266
I just saw this in an init script: echo $&quot;Stopping Apache&quot;  What is that dollar-sign for? My research so far: I found this in the bash manual:  extquote  If set, $'string' and $&quot;string&quot; quoting is performed within ${parameter} expansions enclosed in double quotes.  This option is enabled by default.   ...but I'm not finding any difference between strings with and without the $ prefix: $ echo &quot;I am in $PWD&quot; I am in /var/shared/home/southworth/qed $ echo $&quot;I am in $PWD&quot; I am in /var/shared/home/southworth/qed $ echo $&quot;I am in ${PWD}&quot; I am in /var/shared/home/southworth/qed $ echo &quot;I am in ${PWD}&quot; I am in /var/shared/home/southworth/qed $ echo 'I am in ${PWD}' I am in ${PWD} $ echo $'I am in ${PWD}' I am in ${PWD} $ echo $'I am in $PWD' I am in $PWD
112657
I know I can solve this problem several ways, but I'm wondering if there is a way to do it using only bash built-ins, and if not, what is the most efficient way to do it.  I have a file with contents like  AAA B C DDD FOO BAR   by which I only mean it has several lines and each line may or may not have spaces.  I want to run a command like  cmd AAA "B C DDD" "FOO BAR"   If I use cmd $(&lt; file) I get  cmd AAA B C DDD FOO BAR   and if I use cmd "$(&lt; file)" I get  cmd "AAA B C DDD FOO BAR"   How do I get each line treated a exactly one parameter?