Linked Questions

14
votes
3answers
44k views

How to test if a variable is defined at all in Bash prior to version 4.2 with the nounset shell option?

For Bash versions prior to "GNU bash, Version 4.2" are there any equivalent alternatives for the -v option of the test command? For example: shopt -os nounset test -v foobar && echo foo || ...
21
votes
3answers
5k views

Issue an error when using empty shell variables

Sometimes I use, $PROJECT_HOME/* to delete all files in the project. When the environment variable, PROJECT_HOME is not set (because I did su and the new user doesn't have this environment variable ...
2
votes
3answers
4k views

How do I check whether a variable has been passed to a function in Bash?

I have this: function abash { if [[ -v $1 ]] then atom ~/Shell/$1.sh else atom ~/.bashrc fi } in my ~/.bashrc file, so as to make it easier to use ...
2
votes
1answer
1k views

Differences between declared, defined, and set

In shell, what are the differences between a variable that is declared, defined and set? Does "declared" mean the identifier of the variable exists, but the storage and value of the variable do not? ...
0
votes
3answers
2k views

Checking if line in file exists

I have a bash script which asks a user for the number of CPU cores and saves it to variable named $cores. Now I want to add this variable to .bashrc, so I ask user how many CPU cores he has and then ...
2
votes
1answer
431 views

how to pass a mixture of arguments to a script and only of them optional -argument

I've been exploring this issue for longer than it should take me and finding getopts a very confusing tool. All I want to do is the following. Have a script that I can pass arguments like this $1 $2 ...
0
votes
4answers
84 views

Bash: while loop over numbered variables

My environment can have a flexible number of variables defined with the following format: Var1Value=A Var2Value=B Var3Value=C I now would like to iterate over the variables as long as they exists. ...
1
vote
2answers
94 views

bash: declare/typeset without assignment appears to fail

Using bash 4.3, there seems to be a discrepancy between the manual and actual behaviour: unset string # just to be sure declare string # $? is 0 afterwards declare -p string # fails, 'string: not ...
0
votes
1answer
218 views

What is the definition of an array being set?

The definition for an array being set is from bash manual: An array variable is considered set if a subscript has been assigned a value. The null string is a valid value. Does it mean that an ...
1
vote
1answer
66 views

What are the purpose and use cases of “${parameter:+word}”? [closed]

From Bash manual ${parameter:+word} If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted. It looks like string replacement only when the ...
0
votes
1answer
79 views

Is this a correct way to test if a parameter is declared?

Here is an example from https://unix.stackexchange.com/a/56846/674 A different, bash-specific way of testing whether a variable of any type has been declared is to check whether it's listed in ${!...