Suppose I do the following:  $ echo {10..20} 10 11 12 13 14 15 16 17 18 19 20 $ A=10 $ B=20 $ echo {${A}..${B}} {10..20}   Why does this not expand as it did the first time?  I am trying to set up a "for" loop in a script:  for i in {10..20} do     echo ${i} done 10 11 12 13 14 15 16 17 18 19 20   But if I use variables...  for i in {${A}..${B}} do     echo ${i} done {10..20}   I tried using "eval". Didn't work.  for i in `eval {${A}..${B}}` ...   I tried parentheses. Didn't work.  for i in "{${A}..${B}}" ...   What else can I try, other than  seq ${A} ${B}   ?
16147
How do I iterate over a range of numbers in Bash when the range is given by a variable?  I know I can do this (called "sequence expression" in the Bash ):   for i in {1..5}; do echo $i; done   Which gives:     1    2    3    4    5   Yet, how can I replace either of the range endpoints with a variable? This doesn't work:  END=5 for i in {1..$END}; do echo $i; done   Which prints:     {1..5}
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'
70781
Please explain why 1 4 5 6 is displayed for the last four echo statements?  I hit this by accident once, but I am now curious as to why this behavior occurs.  These statements work as expected (for me).  $ echo [ 9876543210 ]   [ 9876543210 ]  $ echo [237890]   [237890]   These echo statements consistently display 1 4 5 6.  Is there something special about these numbers?  $ echo [9876543210]   1 4 5 6  $ echo [abcd9876543210ghi]   1 4 5 6  $ echo [-123456-]   1 4 5 6  $ echo [-7654321-]   1 4 5 6   Thanks!   The possible duplicate is related and helpful, but not a duplicate. The possible duplicate is from the perspective of an rm command. This question is from the perspective of a perceived "weird behavior" of an echo command. The underlying answer for both is globbing. Someone searching for issues with an echo command would not easily find the rm question, but would more likely land here.
56119
To embrace the DRY (Don’t Repeat Yourself) principle, I sometimes need to share pieces of shell commands in a Makefile. So there is a recipe somewhere in that file like:    shell=/bin/bash # … .ONESHELL: run:     # Create bash sub-shell «cmd» var, holding a built-in test as a string     @cmd='[ $$(grep -iE _dev &lt;/etc/hosts | wc -l) -eq 0 ]'     # "$$" tells make to escape the dollar sign. So echoing …     @echo "$$cmd"     # gives «[ $(grep -iE _dev &lt;/etc/hosts | wc -l) -eq 0 ]» as I expected     # I need this variable to be expanded and interpreted as a real     # built-in square bracket test, so I wrote     @$$cmd &amp;&amp; echo "Pass ! do more magical things …" || true   I expected make to escape $ sign $$cmd ⇒ $cmd which would in turn be expanded within bash context into the bracket test unquoted string … right ? But I get an error instead /bin/bash: line 2: [: too many arguments  Does anybody have an idea of why this error is being raised ? Why bash is not given the bracket test I expect ? [ $(grep -iE _dev &lt;/etc/hosts | wc -l) -eq 0 ] &amp;&amp; echo "Pass!"  Thank you.
52980
I want to set a variable if my condition is true on my Ubuntu system.  This proves that my if-statement is correct:  $ (if [ 1 == 1 ]; then echo "hi there"; fi); hi there   This proves that I can set variables:  $ a=1 $ echo $a 1   This shows that setting a variable in the if-statement DOES NOT work:  $ (if [ 1 == 1 ]; then a=2; fi); $ echo $a 1   Any ideas why? All my  indicates that it should work like this...
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.
9163
I can read the numbers and operation in with:  echo "First number please" read num1 echo "Second number please" read num2 echo "Operation?" read op   but then all my attempts to add the numbers fail:  case "$op" in   "+")     echo num1+num2;;   "-")     echo `num1-num2`;; esac   Run:  First number please 1 Second mumber please 2 Operation? +   Output:  num1+num2   ...or...  echo $num1+$num2;;  # results in: 1+2       ...or...  echo `$num1`+`$num2`;;  # results in: ...line 9: 1: command not found   Seems like I'm getting strings still perhaps when I try add add ("2+2" instead of "4").
20273
As a primarily Java programmer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain why the first one works, but not the second or third?  #works if [ -n $1 ]; then echo "parameterized"; fi  #output: ./links: Zeile 1: [-n: Kommando nicht gefunden. if [-n $1 ]; then echo "parameterized"; fi  #output: ./links: Zeile 1: [: Fehlende `]' if [ -n $1]; then echo "parameterized"; fi
33101
How do I echo one or more tab characters using a bash script? When I run this code  res='       'x # res = "\t\tx" echo '['$res']' # expect [\t\tx]   I get this  res=[ x] # that is [&lt;space&gt;x]
26157
I've a file named addresses.txt :  address1@domain.com address2@domain.com address3@domain.com   I'd like to echo "address1@domain.com.html" using for loop. Here's what I did :  cat addresses.txt | while read address do echo "${address}.html" done   Here's the result :  .htmlss1@domain.com .htmlss2@domain.com .htmlss3@domain.com   Same result if I add a space between the variable and ".html"  What am I miss ?  Thanks
82721
$ a='"apple","ball","cat"' $ a='['$a $ echo $a ["apple","ball","cat" $ a=$a']' $ echo $a b   I'm stumped hard by the result b while I expect to see ["apple,"ball","cat"]. What am I missing here?  This is from bash shell on Mac. Also see it on CentOS 7, while not on Fedora. Can someone please explain?
28963
I thought this would be simple - but it is proving more complex than I expected.  I want to iterate through all the files of a particular type in a directory, so I write this:  #!/bin/bash  for fname in *.zip ; do    echo current file is ${fname} done   This works as long as there is at least one matching file in the directory. However if there are no matching files, I get this:  current file is *.zip   I then tried:  #!/bin/bash  FILES=`ls *.zip` for fname in "${FILES}" ; do     echo current file is ${fname} done   While the body of the loop does not execute when there are no files, I get an error from ls:  ls: *.zip: No such file or directory   How do I write a loop which cleanly handles no matching files?
10618
I am trying to enumerate a range of integers using a variable but having some trouble.  When I type  echo {1..5}   I get  1 2 3 4 5   However, when I type a variable, I am unable to get the enumeration.  For example  $ num=5 $ echo {1..$num} {1..5}   I am stumped as to why the result is not the same.  I have tried using quotes and that has not helped either.  Can someone help or explain?
11519
I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it.  Here's what I've got: #!/bin/bash cd ~/Desktop; thefile= ls -t -U | grep -m 1 &quot;Screen Shot&quot;; echo &quot;Most recent screenshot is: &quot;$thefile;  But that outputs: Screen Shot 2011-07-03 at 1.55.43 PM.png Most recent screenshot is:   So, it looks like that isn't getting assigned to $thefile, and is being printed as it's executed. What am I missing?
28663
I found this line in a korn shell script:  var=${var:-/temp}   I am able to figure it out what it is doing   $ var=/data $ var=${var:-/temp} $ echo $var /data $ var= $ echo $var  $ var=${var:-/temp} $ echo $var /temp   Does it have advantage over checking the value and then assigning?
118424
I'm used to bash's builtin read function in while loops, e.g.:  echo "0 1       1 1       1 2       2 3" |\ while read A B; do     echo $A + $B | bc; done   I've been working on some make project, and it became prudent to split files and store intermediary results. As a consequence I often end up shredding single lines into variables. While the following example works pretty well,  head -n1 somefile | while read A B C D E FOO; do [... use vars here ...]; done   it's sort of stupid, because the while loop will never run more than once. But without the while,  head -n1 somefile | read A B C D E FOO; [... use vars here ...]   The read variables are always empty when I use them. I never noticed this behaviour of read, because usually I'd use while loops to process many similar lines. How can I use bash's read builtin without a while loop? Or is there another (or even better) way to read a single line into multiple (!) variables?  Conclusion  The answers teach us, it's a problem of scoping. The statement   cmd0; cmd1; cmd2 | cmd3; cmd4   is interpreted such that the commands cmd0, cmd1, and cmd4 are executed in the same scope, while the commands cmd2 and cmd3 are each given their own subshell, and consequently different scopes. The original shell is the parent of both subshells.
121958
I've read that you need double quotes for expanding variables, e.g.  if [ -n "$test" ]; then echo '$test ok'; else echo '$test null'; fi   will work as expected, while  if [ -n $test ]; then echo '$test ok'; else echo '$test null'; fi   will always say $test ok even if $test is null.  but then why don't we need quotes in echo $test?