-3

I learned the following command-

$[variable]-If set, access the variable

In this command what type of variable it is talking about?

3
  • 7
    In what context did you learn this? if you are referring to the bash shell, then $[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
  • I learned it from a cheat sheet it is written there as it is written in the question or nothing else – coding_ninza Jan 17 '18 at 13:27
  • 1
    Can you show us that cheat sheet you are talking about? – derHugo Jan 17 '18 at 13:41
2

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
2
  • 1
    Though this doesn't actually address the question. – derHugo Jan 17 '18 at 13:39
  • @derHugo, You are right, I answered only the general question about variables. Now I have added details about $[expression] – sudodus Jan 17 '18 at 15:05
0

$ 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.

Example 1 - variables:

x=hello
echo $x

this will print "hello" in the terminal

Example 2 - commands

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

Example 3 -- Variable command expansion

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
-2

$[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.

10
  • What does if set means here? Is it mean if the path exist – coding_ninza Jan 17 '18 at 13:15
  • if set means if you set the variable. – TheComputerGeek010101001 Jan 17 '18 at 13:18
  • @TheComputerGeek010101001 can you give an example on how/when to use this specific notation? what is the difference to using ${variable} or $variable – Phillip -Zyan K Lee- Stockmann Jan 17 '18 at 13:20
  • I use this command to execute home folder by typing " $[/home/souro]" but it shows error "bash: /home/souro: syntax error: operand expected (error token is "/home/souro")" – coding_ninza Jan 17 '18 at 13:20
  • You want: home=/home/souro Then to reference your home folder, just use $home. – TheComputerGeek010101001 Jan 17 '18 at 13:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.