1. Introduction

Inline shell scripting enhances the flexibility and functionality of shell scripts. To streamline code or dynamically pass information between scripts, understanding inline shell scripting can be crucial.

In this tutorial, we’ll discuss how to send a variable to an inline shell script.

First, we’ll look at the approach of using an environment variable. Next, we’ll explore the use of the export command with an environment variable. Lastly, we’ll look into the here string and here document structures to send a variable to a shell script.

2. Overview of an Inline Shell Script

Inline shell script refers to embedding a shell script directly within another context, such as:

  • larger script
  • configuration file
  • command line

Hence, this approach is often used for short and simple tasks but can become less practical for larger and more complex scripts.

Bash one-liners are examples of inline shell scripts.

3. Using an Environment Variable

To begin with, we can use an environment variable to pass a value to an inline shell script.

Environment variables are set outside scripts but can be accessed by the script through the shell.  Moreover, these variables are part of the environment in which a process runs, so we can use them to pass information to a running process or to configure its behavior.

Let’s use the cat command to take a look at an example script:

$ VAR="Test" sh -c 'echo "Hello $VAR"'

In the above code, we set the value of the VAR environment variable to Test. Next, we run the shell command sh along with -c. The -c option enables us to provide a command as a string.

After that, the embedded command within single quotes echoes the message Hello followed by the value of the VAR environment variable. Thus, the use of single quotes delays variable expansion, ensuring that the command is executed by the new shell.

In summary, this shell script demonstrates the setting of an environment variable and the execution of an inline shell script to display a message including the variable’s value.

4. Using export With sudo

Alternatively, we can use the sudo command to export the environment variable value to a new shell session.

For instance, to export VAR, we’ll preserve the variable using sudo, and pass it to the shell environment:

$ cat exportSudo.sh
#!/bin/bash
export VAR="test"
sudo --preserve-env=VAR sh -c 'echo "This is a $VAR"'

Let’s make the script executable and run it:

$ chmod +x exportSudo.sh
$ ./exportSudo.sh
This is a test

In the above code, we use the export command to assign the value test to the VAR environment variable. When we export a variable like this, it becomes available to subprocesses, not just the current shell.

Next, we employ the sudo command with the –preserve-env option to retain the VAR shared variable. It enables the command after sudo to access the environment variable as if it were defined in that context.

Lastly, we execute a new shell command that echoes a message containing the preserved variable’s value.

To demonstrate the effects of –preserve-env, we can modify exportSudo.sh to omit it:

$ cat exportSudoNoPreserve.sh 
#!/bin/bash
export VAR="test"
sudo sh -c 'echo "This is a $VAR"'

Without using –preserve-env=VAR, the output is incomplete:

$ chmod +x ./exportSudoNoPreserve.sh
$ ./exportSudoNoPreserve.sh
This is a

In this case, the value of VAR isn’t available to the subshell, resulting in an empty output.

5. Using Positional Arguments

Another approach to sending variables to an inline shell script is by using positional arguments.

Positional arguments are values or variables passed to a script or commands in a specific order, enabling flexible customization and automation. Thus, they also enable easier parameter passing and often play a vital role in command-line interfaces.

For instance, we’ll use two variables as positional arguments and print their values:

$ cat posArg.sh
#!/bin/bash
VAR='world'
VAR1='shell'
sh -c 'echo "Hello ${0} ${1}"' "$VAR" "$VAR1"

Now, we make the script executable and run it:

$ chmod +x posArg.sh
$ ./posArg.sh
Hello world shell

In this script, the variables VAR and VAR1 are set to world and shell, respectively.

Following this, we initiate a shell using sh, and pass positional arguments to it. To elaborate, {0} and {1} are arguments passed to the new shell. When the command is executed, it substitutes $VAR for ${0} and $VAR1 for ${1} in the script.

Lastly, when we execute the script, it produces an output of Hello world shell which corresponds to the assigned values of the variables.

6. Using Here String

An alternative method for our needs is to use the here string to send a variable to an inline shell script.

A here string, represented by <<<, simplifies the process of passing a string as input to a command or subshell. It maintains whitespace integrity and enhances code readability by keeping the input string adjacent to the command, providing a concise alternative to other methods of information passing.

Let’s view the code in hString.sh:

$ cat hString.sh
#!/bin/bash
VAR='Hello world'
sh <<< "echo Variable: $VAR"

Now, we execute and check the output of the script:

$ chmod +x hString.sh
$ ./hString.sh
Variable: Hello world

In the above script, we utilized a variable named VAR with the value Hello world. Following this, we use here string to pass the string echo Variable: $VAR to a subshell created by the sh command.

Finally, we echo our results.

7. Using Here Document

Similarly, we can use a here document to transmit a variable to an inline shell script.

A here document is a shell scripting feature that enables the inclusion of multiline text without escaping special characters. Thus, here documents are particularly useful for enhancing the clarity and maintainability of shell scripts.

For instance, to pass a value to an inline shell, we use the redirection operator with EOF:

$ cat hDoc.sh
VAR='This is a test object.'
sh <<EOF
echo Variable: $VAR
EOF

Next, we’ll make the file executable:

$ chmod +x hDoc.sh
$ ./hDoc.sh
Variable: This is a test object.

Within this script, we assign a value to the VAR variable. After that, we use heredoc to pass a value to the inline shell.

Inside this subshell, the echo command prints the value of VAR.

8. Conclusion

In this article, we learned how to send a variable to an inline shell script.

Initially, we explored the use of an environment variable. Next, we discussed the use of the export and sudo commands with an environment variable to print its value in a new shell. Lastly, we learned the use of here string and here document to send values to an inline shell.

Comments are closed on this article!