n=5   for i in {1..$[n]}   do         echo $i   done   it gives:  {1..5}   But I think it should output:  1 2 3 4 5   Why it gives such a strange output?
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}
89143
What does the output of the command below mean? (result is positive integer)  echo $$
122787
Is there a way to print an entire array ([key]=value) without looping over all elements?    Assume I have created an array with some elements:    declare -A array array=([a1]=1 [a2]=2 ... [b1]=bbb ... [f500]=abcdef)   I can print back the entire array with  for i in "${!array[@]}" do echo "${i}=${array[$i]}" done   However, it seems bash already knows how to get all array elements in one "go" - both keys ${!array[@]} and values ${array[@]}.    Is there a way to make bash print this info without the loop?  Edit: typeset -p array does that! However I can't remove both prefix and suffix in a single substitution:  a="$(typeset -p array)" b="${a##*(}" c="${b%% )*}"   Is there a cleaner way to get/print only the key=value portion of the output?
114683
# touch $$ # gzip $$ # gzip --test $$.gz # echo $? 0 # OUT=$(gzip --test $$.gz) # echo $OUT  # if [ -z $OUT ] ; then echo $$ ; fi 26521 # if [ -n $OUT ] ; then echo $$ ; fi 26521 #      from bash(1)      -z string           True if the length of string is zero.    string    -n string           True if the length of string is non-zero.    I'm confused, how is it zero and non-zero at the same time? How does one do check against if key has a value (using bash)?
95685
If I append this:   hi=11 export hi   to the .bashrc and then I su to this user with:   su - bela   then I:   echo $hi 11   Then it's working, the "hi" variable has the value of "11". But. When I:   su -c "echo $hi" bela   the variable "hi" has no value. Why?  (running on CentOS 6)
35787
Say I have this file:  hello world hello world   This program  #!/bin/bash  for i in $(cat $1); do     echo "tester: $i" done   outputs  tester: hello tester: world tester: hello tester: world   I'd like to have the for iterate over each line individually ignoring whitespaces though, i.e. the last two lines should be replaced by  tester: hello world   Using quotes for i in "$(cat $1)"; results in i being assigned the whole file at once. What should I change?
22243
I am having difficulty getting a for loop to take user input in the form of an argument to a function I created. #!/bin/bash number1=&quot;$1&quot;    #Assume user input 5 loops=&quot;$2&quot;      #Assume user input 5 if [[ ${number1} &amp;&amp; ${loops} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..${loops}}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  This script does not run when the user inputs an integer for the variable &quot;loops&quot;, and any number for &quot;number1&quot;. number1 = 5 in this case. It returns the error:  (standard_in) 1: syntax error 5 * {1..5} = /home/$USER/For_Loop_Multiplication.sh: line 9: test: {1..5}: integer expression expected /home/$USER/For_Loop_Multiplication.sh: line 11: test: {1..5}: integer expression expected  If I instead directly input an end bound on the number of loops directly into the script like the following, and number1 = 5, it runs fine: #!/bin/bash number1=&quot;$1&quot;     #Assume user input 5 if [[ ${number1} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..5}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  It returns the result:  5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25  Essentially what I think is happening is that the script seems to be placing the entire range as the value number2, which is defined as the iterator in the for loop. Since the input for the variable &quot;loops&quot; is an integer, there should be no difference between asking the user for a range and placing it in the loop, or simply setting it as a default when I write the script. However, there apparently is a difference... Any insight into solving this problem is appreciated. Thanks.
51242
When I use the following, I get a result as expected:  $ echo {8..10} 8 9 10   How can I use this brace expansion in an easy way, to get the following output?   $ echo {8..10} 08 09 10   I now that this may be obtained using seq (didn't try), but that is not what I am looking for.  Useful info may be that I am restricted to this bash version. (If you have a zsh solution, but no bash solution, please share as well)  $ bash -version GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu)
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.
86322
This is the script  user@linux:~$ cat script.sh  #!/bin/bash for i in `seq $#` do   echo $i done user@linux:~$    Output  user@linux:~$ ./script.sh a b c 1 2 3 user@linux:~$    Desired Output  I would like to get argument value like this .... and not just the number  user@linux:~$ ./script.sh a b c 1 - a 2 - b 3 - c user@linux:~$
79632
I was recently looking at some code that confused me because it works and I didn't expect it to. The code reduces to this example  #!/bin/bash for var; do   echo "$var" done   When run with command line arguments is prints them  $ ./test a b c a b c   It is this, that is (to me) unexpected. Why does this not result in an error because var is undefined ? Is using this considered 'good practice' ?
30464
I have the following bash script:  #!/bin/bash  upperlim=10  for i in {0..10} do echo $i done  for i in {0..$upperlim} do echo $i done   The first for loop (without the variable upperlim in the loop control) works fine, but the second for loop (with the variable upperlim in the loop control) does not.  Is there any way that I can modify the second for loop so that it works?  Thanks for your time.
17181
echo "scale=3;1/8" | bc   shows .125 on the screen. How to show 0.125 if the output result is less than one?
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?
121232
I have this tex: \(\mu_{A}\left(x\right)\in \[0,1  \right\]\)  The issue here is that [ and ] are not showing around 0,1, why is that?  Thanks.
114154
From my experience with modern programming and scripting languages, I believe most programmers are generally accustomed to referring to the first element of an array by 0 as index. Are there any substantial advantages in using 1?  I'm sure I've heard of more languages other than Zsh behaving similarly with arrays; it's fine by me, as it is equally convenient. However, as previously released and widely used shell scripting languages such as ksh and bash all use 0,  why would someone choose to alter this common "standard"?  My immediate answer to my question would be "of course not"; then, the only explanation I can think of regarding this somewhat "exclusive feature" to shells would be "they just did this to show off a bit more their cool shell".  I don't know much of either Zsh or its history though, and there is a high chance my trivial theory about this does not make any sense.  Is there an explanation for this?  Or is it just out of personal taste?
37727
Why does running echo $$ in bash return a number like 7190, while running echo $ only returns a $?