Bash: Setting default values for variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: Setting default values for variables
# 1  
Old 02-25-2018
Bash: Setting default values for variables

I have a variable I want to use in bash script. The user will pass an argument
to the script and I will store it in `arg_fql`. If the user does not pass the variable,
I still never set arg_fql, but I set another variable to a default. However, if the user
passes a value, `arg_fql` will be set to what the user specified

I have used the following code.

Code:
[ -v arg_fql ] && fql="$arg_fql"
fql=${arg_fql:-"0.002 0.003 1 2"}

Would the following have the same effect?

Code:
fql=${arg_fql:-"0.002 0.003 1 2"}

# 2  
Old 02-26-2018
You did not specify the behaviour, if the user passes an argument of length zero. The general convention is to treat this as a "non-existing argument", so I will assume this in my solution, but of course there might be applications where we have to treat these two cases differently.

Assuming that the user passes the optional parameter to your script in $1, you could write:

Code:
if [[ -z $1 ]]
then
  another_variable=default
else
  arg_fql=$1
fi

# 3  
Old 02-26-2018
Whether they produce the same results or not depends on which version of bash you are using.

On versions of bash less than or equal to at least version 3.2.57(1), test -v variable, [ -v variable ], and [[ -v variable ]] will give you an error because -r is not recognized as a valid operator.

In versions of bash where -r is recognized as an operator, the command sequence:
Code:
[ -v arg_fql ] && fql="$arg_fql"
fql=${arg_fql:-"0.002 0.003 1 2"}

will produce the same final results as the command sequence:
Code:
fql=${arg_fql:-"0.002 0.003 1 2"}

but the first sequence will a run little bit slower and consume a little bit more system resources than the second sequence.

Note also that the second sequence is required to work in any shell that tries to conform to the POSIX standard's requirements for parameter expansions (and is, therefore, portable across many shells) while the first sequence uses an extension to the standards and, as far as I know, only works with some versions of ksh and some versions of bash.

Note also that even on shells that recognize -v as an operator, the output produced by:
Code:
[ -v arg_fql ] && fql="$arg_fql" || fql="0.002 0.003 1 2"

is not equivalent to the output produced by:
Code:
fql=${arg_fql:-"0.002 0.003 1 2"}

in cases where arg_fql has been defined to be an empty string before running the above sequences. In this case, the first sequence will set fqi to an empty string while the second sequence will set it to the string 0.002 0.003 1 2.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

3. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

bash -- concatenating values from variables

Hi This is a simple one but I got a lost in translation when doing. What I want to do, given both variables in the example below, to get one value at the time from both variables, for example: 1:a 2:b etc... I need to get this in bash scripting code: varas="1 2 3 4" varbs="a b c d"... (4 Replies)
Discussion started by: ranmanh
4 Replies

6. Shell Programming and Scripting

[BASH] mapping of values from file line into variables

Hello, I've been struggling with this for some time but can't find a way to do it and I haven't found any other similar thread. I'd like to get the 'fields' in a line from a file into variables in just one command. The file contains data with the next structure:... (4 Replies)
Discussion started by: semaler
4 Replies

7. UNIX for Advanced & Expert Users

Setting global variables with BASH/Linux

I am using functions in a script and for some strange reason the EXPORT command doesnt seem to be making my variables global. Anyone got any ideas? I am using one function to pass some output top another using the pipe command, eg Function 1 | Function 2 Function 2 reads the value... (3 Replies)
Discussion started by: gregf
3 Replies

8. UNIX for Dummies Questions & Answers

Setting up variables

Hi all, I have a shell script that sets up the environment for an application running on UNIX - ksh. This script is run using: . ./script_name XX where XX is a parameter. I want to run it from another shell script but when I do it I don't get the envornment variables set up and the prompt... (3 Replies)
Discussion started by: solar_ext
3 Replies

9. UNIX for Advanced & Expert Users

setting some variables

i have a file .NAMEexport MY_NAME=JOE when i do this at the command prompt #. .NAME $echo MY_NAME $JOEi created a script called Run.sh . .NAME At the command prompt i did #sh Run.sh #echo $MY_NAMEit returns nothing. What have i missed out? (7 Replies)
Discussion started by: new2ss
7 Replies

10. UNIX for Dummies Questions & Answers

Query regarding alias and setting bash as a default script

Hi All, I am setting bash as my working shell in my .profile file. So I have written a line : bash as the list line in my .profile I want to use alias as follows: alias me='who am i' When i log in, as expeced I enter the bash shell but alias doesn't work. Is it because the alias is defined... (1 Reply)
Discussion started by: VENC22
1 Replies
Login or Register to Ask a Question