Questions tagged [variable]

A variable is a name, if correctly chosen with a symbolic meaning, that holds a value or values. Use this tag if your question is specific on the use of variables on shell scripting (if you want to ask about variables in programming languages you should probably ask on StackOverflow)

Entry on wikipedia on variables.

1192 questions
646
votes
4 answers

Using "${a:-b}" for variable assignment in scripts

I have been looking at a few scripts other people wrote (specifically Red Hat), and a lot of their variables are assigned using the following notation VARIABLE1="${VARIABLE1:-some_val}" or some expand other variables VARIABLE2="${VARIABLE2:-`echo…
Justin Garrison
  • 7,359
  • 4
  • 20
  • 21
221
votes
3 answers

Are there naming conventions for variables in shell scripts?

Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables local to the script?
Garrett Hall
  • 5,121
  • 5
  • 19
  • 12
190
votes
6 answers

how can I add (subtract, etc.) two numbers with bash?

I can read the numbers and operation in with: echo "First number please" read num1 echo "Second number please" read num2 echo "Operation?" read op but then all my attempts to add the numbers fail: case "$op" in "+") echo num1+num2;; "-") …
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
173
votes
6 answers

How can we run a command stored in a variable?

$ ls -l /tmp/test/my\ dir/ total 0 I was wondering why the following ways to run the above command fail or succeed? $ abc='ls -l "/tmp/test/my dir"' $ $abc ls: cannot access '"/tmp/test/my': No such file or directory ls: cannot access 'dir"': No…
Tim
  • 98,580
  • 191
  • 570
  • 977
122
votes
3 answers

How can I assign the output of a command to a shell variable?

I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it.  Here's what I've got: #!/bin/bash cd ~/Desktop; thefile= ls -t -U | grep…
Nathan G.
  • 1,368
  • 2
  • 9
  • 8
117
votes
12 answers

How do I check if a variable exists in an 'if' statement?

I need to check a variable's existence in an if statement. Something to the effect of: if [ -v $somevar ] then echo "Variable somevar exists!" else echo "Variable somevar does not exist!" And the closest question to that was this, which…
user104976
90
votes
10 answers

How to assign a string value to a variable over multiple lines while indented?

The issue: I need to assign a variable a value that is decently long. All the lines of my script must be under a certain number of columns. So, I am trying to assign it using more than one line. It's simple to do without indents: VAR="This…
JamesL
  • 1,260
  • 1
  • 13
  • 19
77
votes
5 answers

Pass shell variable as a /pattern/ to awk

Having the following in one of my shell functions: function _process () { awk -v l="$line" ' BEGIN {p=0} /'"$1"'/ {p=1} END{ if(p) print l >> "outfile.txt" } ' } , so when called as _process $arg, $arg gets passed as $1, and used as a…
branquito
  • 1,017
  • 1
  • 9
  • 17
74
votes
8 answers

Assigning exit code to a shell local variable

#!/bin/bash function0() { local t1=$(exit 1) echo $t1 } function0 echo prints empty value. I expected: 1 Why doesn't t1 variable get assigned the exit command's return value - 1?
user93868
72
votes
2 answers

In Bash scripting, what's the meaning of " $! "?

If I'm assigning a variable with temp=$! what would it be its value?
Chen A.
  • 1,303
  • 2
  • 11
  • 14
71
votes
2 answers

In bash scripting, what's the different between declare and a normal variable?

In bash scripting: we create variable by just naming it: abc=ok or we can use declare declare abc=ok what's the difference? and why does bash make so many ways to create a variable?
lovespring
  • 2,051
  • 1
  • 21
  • 22
66
votes
2 answers

How do I assign a value to a BASH variable if that variable is null/unassigned/falsey?

I'm looking for the equivalent to this JS assignment: FOO = FOO || "I must have been falsey!";
Magnus
  • 832
  • 1
  • 6
  • 14
64
votes
5 answers

Scope of Local Variables in Shell Functions

After reading 24.2. Local Variables, I thought that declaring a variable var with the keyword local meant that var's value was only accessible within the block of code delimited by the curly braces of a function. However, after running the following…
maddouri
  • 741
  • 1
  • 6
  • 7
63
votes
5 answers

Variable definition in bash using the local keyword

I'm learning bash scripting and found this on my /usr/share/bash-completion, line 305: local cword words=() What does it do? All tutorials online are just in the format local var=value
Alexandre Santos
  • 1,023
  • 2
  • 11
  • 16
58
votes
6 answers

Escape a variable for use as content of another script

This question is not about how to write a properly escaped string literal. I couldn't find any related question that isn't about how to escape variables for direct consumption within a script or by other programs. My goal is to enable a script to…
Walf
  • 1,254
  • 1
  • 14
  • 19
1
2 3
79 80