Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass empty variable in bash

My problem:

#!/bin/bash  function testFunc(){     echo "param #1 is :" $1     echo "param #2 is :" $2 }  param1="param1" param2="param2"  testFunc $param1 $param2 

This way the output is:

param #1 is : param1 param #2 is : param2 

But when I set param1 to empty string:

param1="" 

Then the output is the following:

param #1 is : param2 param #2 is : 

I guess the problem is that when the first parameter is empty, it's not declared, so it actually doesn't get passed as a function parameter.

If that is the problem, then is there a way to declare a variable "empty string" in bash, or is there any workaround to get the expected behavior?

Note: It works as expected if I call the function like this:

testFunct "" $param2 

But I want to keep the code clean.

UPDATE:

I recently discovered the -u flag which raises an error in case an unbound variable is about to be used.

$ bash -u test.sh param #1 is : param1 test.sh: line 5: $2: unbound variable 
like image 918
kupsef Avatar asked Oct 15 '13 08:10

kupsef


People also ask

How do you declare an empty variable in bash?

Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty"

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What is empty string in bash?

Check if String is Empty using -z String Operator -z string operator checks if given string operand's size is zero. If the string operand is of zero length, then it returns true, or else it returns false. The expression to check if string is empty is [ -z "$s" ] where s is string.

Can you pass variables in shell script?

In a shell script, you can pass variables as arguments by entering arguments after the script name, for example ./script.sh arg1 arg2 . The shell automatically assigns each argument name to a variable. Arguments are set of characters between spaces added after the script.

How to check for an empty or null variable using Bash?

The following bash script example we show some of the way how to check for an empty or null variable using bash: Save the above script into eg. check_empty.sh and execute with our without command line arguments: Furthermore, the execution of the above script with a command line argument will trigger opposite results:

How do I pass an empty string as an argument?

When you type program "" into the shell, at the operating system level, the program receives an empty C language string: a pointer to a null byte. There are no quotes. You can pass the argument "" (a two-character string made of two double quotes) but that is not an empty argument. A way to do that is, for instance, '""': wrap it in single quotes.

How do you pass multiple arguments in a bash script?

You can pass more than one argument to your bash script. In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3 … The second argument will be referenced by the $2 variable, the third argument is referenced by $3,.. etc.

How to check if a parameter is empty in a set?

Another best practice is to check the number of parameters passed using $#. However, this does not solve the problem of one empty parameter, how would you know if the param1 is empty or param2 is empty. Therefore both checks are good parctices. One can add set -o nounset in the body a bash file to achieve the same effect as bash -u does.


1 Answers

On the first case you call the script with testFunct param2. Hence, it understands param2 as the first parameter.

It is always recommendable to pass parameters within quotes to avoid this (and to be honest, for me it is cleaner this way). So you can call it

testFunct "$param1" "$param2" 

So to pass an empty variable you say:

testFunct "" "$param2" 

See an example:

Given this function:

function testFunc(){     echo "param #1 is -> $1"     echo "param #2 is -> $2" } 

Let's call it in different ways:

$ testFunc "" "hello"    # first parameter is empty param #1 is ->  param #2 is -> hello  $ testFunc "hey" "hello" param #1 is -> hey param #2 is -> hello 
like image 112
fedorqui 'SO stop harming' Avatar answered Oct 01 '22 10:10

fedorqui 'SO stop harming'