Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an empty string with double quotes to a bash script?

Tags:

bash

shell

I'm trying to write a script that will accept as additional arguments on the command line an empty string with double quotes. I want the script to pass those arguments along with the double quoted empty string if that is provided, but instead the command interpreter seems to interpret such an argument as an empty string and strips away the quotes. Is there any way to do this?

As a simple example I would have in the file script.sh:

#!/bin/bash
/home/myapp $1 $2

If I run at the prompt:

$ ./script.sh arg1 ""

The script just executes "/home/myapp arg1", but misses/ignores the second argument (""). I want it to preserve this empty string and instead execute: /home/myapp arg1 ""

like image 789
Josh Avatar asked Apr 26 '14 00:04

Josh


People also ask

How do I pass an empty string in bash?

You can quickly test for null or empty variables in a Bash shell script. You need to pass the -z or -n option to the test command or to the if command or use conditional expression. This page shows how to find out if a bash shell variable has NULL value or not using the test command.

How do you pass a double quote in bash?

Single quotes(') and backslash(\) are used to escape double quotes in bash shell script. We all know that inside single quotes, all special characters are ignored by the shell, so you can use double quotes inside it. You can also use a backslash to escape double quotes.

Why are there double quotes in the string command in Linux?

In command "string", the double-quotes are syntactic; they change how string is parsed by the shell, but are not actually passed to the command (and cannot be required by the command). Show activity on this post. And no, the set -x output is not (exactly) what the command receives.

How to replace double quotes with empty string in JavaScript?

There are numerous ways to replace double quotes with empty string. We are going to use the simplest approach which involves the usage of the regex pattern as well as replace () method. The replace () method searches the string for a particular value or a regex pattern and it returns a new string with the replaced values.

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.

What is preserved by the shell when using double quotes?

Also, all the white spaces enclosed within double quotes are preserved by the shell. How to use single quotes in Bash Shell Script? How to escape single quotes in Bash Shell Script?


1 Answers

You are correctly passing an empty string argument to the script.

It is the script that is messing it up:

#!/bin/bash
/home/myapp $1 $2

The script is not protecting the expansion of $1 and $2 from word-splitting. This means that if $1 and $2 contain multiple words, those turn into individual arguments, and if either of them expand to nothing, they simply disappear.

This should be:

#!/bin/bash
/home/myapp "$1" "$2"

In general, you can make your script pass all of its arguments to the invoked program, like this:

/home/myapp "$@"

The quotes are just shell syntax; they are not part of the argument data itself. 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.

The only reason to do things like that is when you're manipulating shell syntax at a meta-level: passing around pieces of shell script source code, such as quoted tokens, empty or otherwise. The shell has a command called eval which takes source code as an argument (or multiple arguments) and evaluates it.

empty_shell_string_syntax='""'    # this variable holds the characters ""

eval empty_var=$empty_shell_string_syntax   # empty_var gets set to empty string

before eval is invoked, the command line is subject to expansion. That expansion removes the syntax $empty_shell_string_sytnax and replaces it with the contents, the characters "". So then eval gets the string empty_var="". It evaluates this, and so empty_var is set to the empty string, as the syntax indicates.

like image 190
Kaz Avatar answered Sep 20 '22 02:09

Kaz