This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
Can anyone tell me why the following:
#!/bin/bash
TEST=$(echo '*** this is a test ***')
echo $TEST
...outputs a listing of the current directory, then "this is a test", then another listing of the current directory?
Background: I have some output that I'm putting in a variable, and then I want to do several different greps on the contents of that variable, but the echo is inserting all this extra stuff that shouldn't be there.
This is on OS X 10.11.4.
globbing in action! * expands in the second echo. You have to double quote it to prevent expansion.
echo "$TEST"
see this for a related answer
Related
This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 1 year ago.
I'm having a hard time figuring out how to use a $var inside curly braces {}..
#!/bin/bash
read -p "How many files must there be built?" numb
for n in {1..$numb}
do
echo "Building file $n"
touch file$n.txt
done
This would result in:
How many files must there be built?8
Building file {1..8}
I've tried enclosing the $numb inside backticks or double quotes but to no avail..
You could use this syntax for your loop:
#!/bin/bash
read -p "How many files must there be built?" numb
for ((i=1; i<=$numb; i++)); do
echo "Building file $i"
touch file$i.txt
done
It's less fancy, but it works.
This question already has answers here:
Why doesn't ''var=value echo $var'' emit value? [duplicate]
(1 answer)
In Bash, why `x=100 echo $x` doesn't print anything? [duplicate]
(2 answers)
Closed 3 years ago.
I am currently trying to familiarise myself with shell line and command execution.
Could anybody explain to me the following behaviour? Why does shell only register an assignment variable when there is a separator before the command? Can assignment not be made in the same command?
sh-3.2$ x=5 echo ${x}
sh-3.2$ x=5; echo ${x}
5
sh-3.2$ x=5 && echo ${x}
5
When you don't have the separator, you're setting x as an environment variable while echo runs (only for the duration of that single command), without defining a shell variable for use in evaluating expansions before the command is started (which is what's happening in the x=5; echo "$x" case).
However, echo doesn't change its output based on which environment variables are defined (except sometimes very specific environment variables intended to modify its behavior -- POSIXLY_CORRECT and the like), so this has no visible effect.
This question already has answers here:
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
Closed 5 years ago.
I am using MacBook pro terminal to execute a shell script. It loops through a text file and create filenames based on each line in the file.
#!/bin/bash
year=2010
list=list_test.txt
mydir=thisdir
i=1 # counter
while read line
do
echo $i $line
file1=`echo $mydir/file_$year_$line_test.tif`
file2=`echo $mydir/file_$year_$line_test.tif`
echo $file1 $file2
i=$(($i+1))
done < $list
However, the output is peculiar:
1 17019
thisdir/file_.tif thisdir/file_.tif
2 17029
thisdir/file_.tif thisdir/file_.tif
3 17039
thisdir/file_.tif thisdir/file_.tif
Within the loop, bash does not recognize some variables, like "year" which is a global, and "line" which is read from the text file. The text file is as below:
17019
17029
17039
Another script with exactly the same manner works very well. This is mysterious to me now.
Any help or comments are extremely appreciated! Thanks very much!
_ is a valid character for an identifier, but you want to use it as a literal character in the file name. You need to use the full form of parameter expansion, ${x} instead of $x.
(Also, the command substitution isn't necessary.)
file1=$mydir/file_${year}_${line}_test.tif
file2=$mydir/file_${year}_${line}_test.tif
This question already has answers here:
Check if a file exists with a wildcard in a shell script [duplicate]
(21 answers)
Closed 6 years ago.
I wish to be able to check if file exist.
if [ -f "/var/run/screen/user/*.$InstanceName" ]; then
echo -e "screen instance exist"
fi
but the wilcard / joker don't work
How I can pass it ?
Your wildcard doesn't work because it's quoted. Unquoting it however might break the [ command as it only expects one filename argument, and if two or more files wore globbed it would break.
In bash you can use compgen that will generate a list of files matching the globbing pattern, it will also set proper exit status if no globs are found, it is a hack? I don't know, but it could look like it:
if compgen -G "/var/run/screen/user/*/$InstanceName" > /dev/null; then
printf "screen instance exist\n"
fi
This question already has answers here:
Variables getting reset after the while read loop that reads from a pipeline
(3 answers)
Closed 7 years ago.
I have 3 lines in file /root/backuplist.txt.
The first echo prints perfectly, but the last one prints an empty line; I'm not sure why. Somehow, the $DIRS value is getting unset.
#!/bin/bash
cat /root/backuplist.txt |
while read line
do
DIRS+="$line "
echo $DIRS
done
echo $DIRS
Problem is use of pipe here, which is forking a sub-shell for your while loop and thus changes to DIRS are being made in the child shell that are not visible in the parent shell. Besides cat is unnecessary here.
Have it this way:
#!/bin/bash
while read -r line
do
DIRS+="$line "
echo "$DIRS"
done < /root/backuplist.txt
echo "$DIRS"