This question already has answers here:
How to echo "$x_$y" in Bash script?
(4 answers)
When do we need curly braces around shell variables?
(7 answers)
Closed 5 years ago.
I am new at this and I'll simplify my problem. I doing the following script:
echo "Insert number of elements:"
read elements
echo "Insert md number:"
read sim
echo "uni_$elements_md$sim.tpr" >> doc.bash
But when I run the doc.bash file it only shows inside:
uni_4.tpr
Please help me, how must I do my echo so that if I put elements as 14 and sim as 4 (just to put an example), it displays:
uni_14_md4.tpr
Thanks.
Related
This question already has answers here:
What's the magic of "-" (a dash) in command-line parameters?
(5 answers)
Closed 3 years ago.
I recently had to use some code that was given to me, and in it the is this line
cat file -
Did someone know what the - mean.
The - means "read from standard input" (this is a POSIX convention). Try this in your shell and you'll see cat repeat everything you type:
$> cat -
This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
i am trying to get value of variable by given string:
Running this code on Jenkins and its get "bad substitution"
in regular shell it works.
example:
param1="hello"
param2="world"
PARAMS="param1 param2"
for p in $PARAMS;do
echo ${!p}" "
done
what the best way to make it work in Jenkins too.
You must use with $ sign before the name of the variable:
PARAMS="$param1 $param2"
for p in $PARAMS;do
echo ${p}" "
done
This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 3 years ago.
Consider the following code:
name=John
echo ${name}
It prints "John", just as expected. Now consider this code:
name=John
echo $name
Again, this code prints "John" just as expected. Both codes work fine.
But I wonder is there any difference between the two, e.g. compatibility?
In your case, there is no difference.
In this case, there is:
name=John
echo ${name}Doe
echo $nameDoe
Read more: here
This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Variable variable assignment error -"command not found"
(1 answer)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
So I've got a shell script to do some lazy stuff for if the directory isn't changing for a user. It's below. Essentially, it should be an if statement that if the user enters "default" for the directory, it'll pull them to the default directory for the files. However, I'm getting a command not found on line 16, which is the reassignment statement.
The entire if statement below:
if [ $directory = "default" ];
then
echo Enter your ldap:
read $ldap
$directory = "/usr/local/home/google/${ldap}/Downloads"
fi
I've tried doing it without the dollar sign too...nothing. What's going on here? New to shell, couldn't find this question asked before either.
This is how you should assign a value to a variable in shell:
directory="/usr/local/home/google/${ldap}/Downloads"
No dollar ($) sign.
No space around equal (=) sign.
Also, you should wrap your variables inside double quotes ("). This way, you avoid errors with undefined variables, arguments with spaces, etc.
That gives us:
if [ "$directory" = "default" ]
then
echo "Enter your ldap:"
read $ldap
directory="/usr/local/home/google/${ldap}/Downloads"
fi
This question already has an answer here:
Reading input in bash in an infinite loop and reacting to it
(1 answer)
Closed 6 years ago.
I need to input into variable hyperlink and then download it via wget
Link is:
http://st-im.xyz.com/im/poster/2/1/0/xyz.com-Qwerty-2107544.jpg
But it seems like bash is not writing my link into variable. So, how can I fix this?
Code:
read $link
echo "Link is"
echo $link
wget "$link"
Output:
http://st-im.xyz.com/im/poster/2/1/0/xyz.com-Qwerty-2107544.jpg # This is my input
Link is
http://: Invalid host name.
You are reading the value wrong.
It should be:
read link
$variable is for variable substitution. (Retrieving the value). Not for assigning the value.