I learned the following command-
$[variable]-If set, access the variable
In this command what type of variable it is talking about?
I learned the following command-
$[variable]-If set, access the variable
In this command what type of variable it is talking about?
You can use variables to store and manipulate numbers or strings. bash
is the standard shell in Ubuntu, and it uses variables like the following examples with a variable with the name var1
:
Set the variable (give it a value)
var1="Hello World"
Print the variable to the screen
echo "$var1"
There are several useful tutorials, that you find if you search the internet, for example with the search string bash variable tutorial, for example
ryanstutorials.net/bash-scripting-tutorial/bash-variables.php
The manual man bash
writes the following about Arithmetic Expansion,
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:
$((expression))
The old format
$[expression]
is deprecated and will be removed in upcoming versions of bash.
Evaluating an expression and printing to the screen,
$ echo $((7*8)) # recommended
56
$ echo $[6*9] # works now, but deprecated
54
$[expression]
– sudodus
Jan 17 '18 at 15:05
$ means to substitute the value of the following expression. I can be used with variables or commands. To use it with commands, parenthesis are placed around the command.
x=hello
echo $x
this will print "hello" in the terminal
Say I have a file named hello.txt with the contents:
Hello
world.
Good
Morning
Then I can do:
echo $(cat hello.txt)
the output would be:
Hello world. Good Morning
This can also be used to allow user input in a script. For example say I have a script that executes a file, I can allow the user to specify where the file is and save it as path. Then do:
exec $path/file.sh
If path="/home/me", then the command would expand to:
exec /home/me/file.sh
$[variable]
is referring to a variable that you set with the following command:
VARIABLE=<path>
<path>
can be anything. It can be a path to another drive, a file server, a document, an application, and even a shortcut.
${variable}
or $variable
– Phillip -Zyan K Lee- Stockmann
Jan 17 '18 at 13:20
home=/home/souro
Then to reference your home folder, just use $home
.
– TheComputerGeek010101001
Jan 17 '18 at 13:26
Value=123456
and your variable=123, than echo "Value=$variable456"
would fail and you had to use echo "Value=${variable}456"
– derHugo
Jan 17 '18 at 13:37
${}
and $[]
- steeldriver posted a comment on the question, which explains the difference.
– Phillip -Zyan K Lee- Stockmann
Jan 17 '18 at 13:41
$[expression]
is a (deprecated) form of arithmetic evaluation, so$[variable]
would be the simplest instance of that, with expresssion = variable. See for example Difference between let, expr and $[]. It sounds to me like what you actually saw was${variable}
– steeldriver Jan 17 '18 at 13:22