Defining and calling variables in shell script - shell

I want to define variable in shell script as:
value1 = 40 (this can be number or character)
and want to use as in a text like:
$value1_position.xyz (I basically want 40_position.xyz)
How do I do this?

this should do:
${value1}_position.xyz
beware that the variable should be declared with this syntax
value1=40
notice the absence of spaces around the =

To define a variable, simply make sure there are no spaces between the variable name and value
value1=40
To use that variable in bash substitution, creating what you want, use the $ replacement symbol like so:
${value1}_position.xyz
To append that to your text file
echo "${value1}_position.xyz" >> file.txt

Related

Bash substitute variable in command

myDir = 'apple'
If I have the above variable, what functionality can I use to use it within a command? (I'm guessing it's some type of substitution - so I'd like to know what it's called if so)
How could I use the above variable to do an ls /home/applefruit, as obviously ls /home/$myDirfruit does not work.
First of all, your variable declaration is not right. There must be no spaces around = in declaration:
myDir='apple'
Now, ls /home/$myDirfruit did not work because myDirfruit is being treated as the variable name instead of just myDir. You need to use {} to enclose the variable name when the name is being followed by valid variable name constituent character:
ls /home/${myDir}fruit
would be expanded to:
ls /home/applefruit
Also if you have spaces in variable name e.g. myDir='foo bar', use quotes around variable:
ls /home/"${myDir}"fruit

Why are shell script variables declared without a preceding `$`?

I noticed that in shell script when we declare a variable, the preceding dollar sign is not needed, although when we want to access this variable later we should add a dollar sign in front of this variable name.
just like:
#!/bin/sh
VAR_1=Hello
VAR_2=Unix
echo "$VAR_1 $VAR_2"
This is different from other languages, like Perl we will always have the preceding dollar sign with the variable name, I just want to know any good reason for shell script to do it in this way, or it's just a convention...?
Shell is a different language than Perl is a different language than C++ is a different language than Python. You can add "with different rules" to each of the languages.
In shell an identifier like VAR_1 names a variable, the dollar sign is used to invoke expansion. $var is replaced with var's content; ${var:-foo} is replaced with var's content if it is set and with the word foo if the variable isn't set. Expansion works on non-variables as well, e.g. you can chain expansion like ${${var##*/}%.*} should leave only a file base name if var contains a file name with full path and extension.
In Perl the sigil in front of the variable tells Perl how to interpret the identifier: $var is a scalar, #var an array, %var a hash etc.
In Ruby the sigil in front of the varible tells Ruby its scope: var is a local variable, $var is a global one, #var is an instance variable of an object and ##var is a class variable.
In C++ we don't have sigils in front of variable names.
Etc.
In the shell, the $ sign is not part of the variable name. It just tells the shell to replace the following word with the contents of the variable with the same name, i.e. $foo means "insert the contents of the variable foo here".
This is not used when assigning to the variable because there you explicitly don't want to insert the old contents; you want to use the variable itself (in some ways this is similar to dereferencing pointers).
It's basically a syntactical convention.
DOS/.bat file syntax works the same way.
1) to create a variable, no metacharacter.
2) to "dereference" the contents of the variable, use the metacharacter.
DOS:
set VAR=123
echo %VAR%

Using a variable containing spaces as a single argument

Think you have a variable that contains a string of text with spaces inbetween and you want to use that as input arguments for another script. How would you go about passing the variable content without worrying about the space chars?
The following doesn't work:
VAR1=hello\ world
#... do something else
./a_script.sh $VAR1
Use double quotes:
VAR1=hello\ world
#... do something else
./a_script.sh "$VAR1"

Bash Color Variable Output

I've got a variable, let's say $x and it holds the value of website.com. I want to be able to call the variable and apply shell color to it like so:
echo -e '\033[1;32m$x:\033[0m';
The problem is not the color, however, it's how the script it interpretting the output. So the output I'm getting is:
$x:
I need the output to obviously be the string in the variable, and not the variable name. Is there any way around this issue?
You need to use " instead of '.
So it should be: echo -e "\033[1;32m$x:\033[0m";
Variables are generally interpolated inside double quotes.

Shell script input containing asterisk

How do I write a shell script (bash on HPUX) that receives a string as an argument containing an asterisk?
e.g. myscript my_db_name "SELECT * FROM table;"
The asterisk gets expanded to all the file names in the current directory, also if I assign a variable like this.
DB_QUERY="$2"
echo $DB_QUERY
The asterisk "*" is not the only character you have to watch out for, there's lots of other shell meta-charaters that can cause problems, like < > $ | ; &
The simple answer is always to put your arguments in quotes (that's the double-quote, " ) when you don't know what they might contain.
For your example, you should write:
DB_QUERY="$2"
echo "$DB_QUERY"
It starts getting awkward when you want your argument to be used as multiple parameters or you start using eval, but you can ask about that separately.
You always need to put double quotes around a variable reference if you want to prevent it from triggering filename expansion. So, in your example, use:
DB_QUERY="$2"
echo "$DB_QUERY"
In the first example, use single quotes:
myscript my_db_name 'SELECT * FROM table;'
In the second example, use double quotes:
echo "$DB_QUERY"

Resources