-3

I learned the following command-

$[variable]-If set, access the variable

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

3 Answers3

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
sudodus
  • 46,324
  • 5
  • 88
  • 152
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
ravery
  • 6,874
-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.