wiki/help/linux/bash/script/variable.md

988 B

variables.

declare.

plain.

inside functions you can declare functions like this:

variable1="hello"       # global variable.
local variable2="world" # local variable.

it is preferable to declare variables as local because this way they won't be left after the function execution.

from output.

you can store another program's output into variable.

local jpegs=$(ls | grep .jpg$)

use.

local myvar="boom!"
echo "this is it: ${myvar}" # better way.
echo "this is it: $myvar"   # simplified way.

you almost always want to wrap string variables in double quotes when reading their values. if you don't wrap them like "$var1" their content is not escaped and thus interpreted as a part of the function itself. you usually don't wrap them if you pass variable content "as is" to the command arguments i.e. if you want to preserve all flags and special characters like that:

local arguments=-n foo -i bar
myprogramm $arguments