More

    Bash How to Execute a Command in a Variable? – Linux Hint

    Bash scripts will be created in a wide range of alternative ways and most of us are aware of executing the straightforward instructions inside a Bash script. Nevertheless, these instructions may also be encapsulated inside the variables in Bash. This course of is called command substitution and it’s typically used to retailer the output of a command in a variable so that you just should not have to run that command explicitly time and again moderately you’ll be able to merely entry that variable to get the output of that command everytime you need. On this article, we’ll present you the way this may be carried out.Observe: All of the situations demonstrated under have been carried out on Ubuntu 20.04. Nevertheless, they may work precisely in the identical means with some other taste of Linux as nicely.

    Methodology of Executing a Command in a Variable in Bash:

    For demonstrating the tactic of executing a command in a variable in Bash, we’ll current to you three completely different situations that are as follows:

    Executing the “echo” Command Saved in a Variable:

    That is the only situation by which our objective is to execute the echo command which is saved in a variable. For making it to occur, you’ll have to comply with the collection of steps talked about under:

    Step # 1: Making a Bash Script:

    • You must create a Bash Script in your Dwelling folder for which that you must click on on the File Supervisor icon as you’ll be able to see from the next picture:

    • Now discover any house in your Dwelling folder and right-click on it to launch a menu. Choose the New Doc choice from this menu after which select the Empty Doc choice from the sub-cascading menu. Doing this can create a brand new doc in your Dwelling folder. Now rename this newly created doc with any title of your alternative adopted by the .sh extension. In our case, we’ve got named it as CommandVar.sh.

    • For writing a Bash script on this file, double click on on it to open it after which sort the script proven within the picture under in your Bash file. Right here, the primary line of the script i.e. “#!/bin/bash” exhibits that this file is in actual fact a Bash file. Then we’ve got created a variable named “check” and have assigned it the worth “$(echo “Hello there!”)”. Everytime you need to retailer the command in a variable, you must sort that command preceded by a “$” image. On this case, we wished to retailer the “echo” command within the “check” variable so we’ve got merely typed the “echo” command adopted by a random message and have enclosed it in spherical brackets, and positioned a “$” image earlier than it. So now, if we need to execute this “echo” command, we should entry the “check” variable. Subsequently, to confirm if the “echo” command saved within the “check” variable will be efficiently executed or not, we’ve got printed the output of the “check” variable on the terminal by making use of one other “echo” command. After typing this script, that you must save your file and shut it.

    Step # 2: Executing the Bash Script through the Terminal:

    • Now you must execute this script through the terminal. So, open the terminal in Ubuntu 20.04 after which sort the next command in it:

    • When you’ll press the Enter key to execute this command, it is possible for you to to see the next output in your terminal. Right here, the highlighted portion of the output is the output of the “echo” command that was saved within the “check” variable.

    Executing the “seq” Command Saved in a Variable:

    On this situation, we’ll print a sequence of numbers by utilizing the “seq” command saved in a variable. For inflicting it to occur, we’ll modify the Bash script created above by performing the next steps:

    Step # 1: Modifying the Bash Script Created above:

    • Open the Bash file that you’ve created within the methodology above and kind the next script in it. Right here, we’ve got created a variable named “sequence”. Our objective is to print the numbers from 1 to 10 whereas utilizing the “seq” command. For doing that, we’ve got assigned the worth “$(seq 1 10)” to the “sequence” variable. You may also specify some other vary of numbers of your alternative if you’d like. The primary quantity after the “seq” command signifies the decrease certain of the sequence whereas the second quantity refers back to the higher certain. After typing this script, save your file and shut it.

    Step # 2: Executing the Modified Bash Script through the Terminal:

    • Now execute your Bash script in the identical method as defined above and it is possible for you to to see the desired sequence in your terminal as proven within the picture under:

    Executing ‘pwd’ Command Saved in a Variable:

    You may also print your working listing by making use of the “pwd” command saved in a variable. To display this, we’ll modify the Bash script created above but once more by following the steps talked about under:

    Step # 1: Modifying the Bash Script Created above:

    • Open the Bash file that you’ve simply modified after which sort the script proven within the following picture in it. On this script, we’ve got created a variable named “working_directory” and have assigned it the worth “$(pwd)”. The “pwd” command will merely retailer its output i.e. the present working listing within the “working_directory” variable. For making certain whether or not the “pwd” command has been appropriately executed or not, we’ve got printed the worth of the “working_directory” variable on the terminal by utilizing the “echo” command. Now save this file after which shut it after typing the modified Bash script in it.

    Step # 2: Executing the Modified Bash Script through the Terminal:

    • Now execute this Bash script in the exact same method as defined above. The output of this Bash script will present you the present working listing. The highlighted portion of the output is in actual fact, the output of the “pwd” command.

    Conclusion:

    This text offers an excellent concept of how one can execute a command that’s saved inside a variable in Bash and might get the identical output as you’ll have gotten in case you ran the command independently.

    bash redirect output to variable,bash echo variable in string,bash use variable in command,store output in string bash,bash command into string,how to store query result in variable in unix,brian fox,z shell,bash vs shell,bash operator,learn bash,effective bash,learn bash in,bash file extension,bash scripting and shell programming,github bash scripts,bash pipe,linux tutorial medium,ubuntu how to navigate in terminal,ubuntu command line list,ubuntu the complete guide,ls in ubuntu,tail command ubuntu,100 shell programs in unix pdf,shell script practical examples,advanced shell script examples,unix shell scripting automation examples,shell scripting practice exercises,android shell script examples,linux commands cheat sheet 2020 pdf,kali linux cheat sheet pdf,cheatography linux,linux cheat sheet wallpaper,ubuntu cheat sheet 18.04 pdf,linux cheatbook,how to store the output of a command to a variable in shell script,shell script assign command output to variable,bash execute command with variable as parameter,bash execute command in variable,bash command output to variable multiple lines,assigning command output to variable bash,bash set variable and run command

    Recent Articles

    Unravel the XDR Noise and Recognize a Proactive Approach

      Cybersecurity professionals know this drill nicely all too nicely. Making sense of heaps of info and noise to entry what actually issues. XDR (Prolonged Detection & Response) has been a technical acronym thrown round within the cybersecurity business with many notations and...

    PLATYPUS: Hackers Can Obtain Crypto Keys by Monitoring CPU Power Consumption

      Researchers have disclosed the small print of a brand new side-channel assault technique that can be utilized to acquire delicate data from a system...

    The Container configurations in Amazon ECS

      Revealed: November 7, 2020 | Modified: November 7, 2020 | Zero views A fast put up on superior container configurations in Amazon ECS. ECS container superior...

    Antivirus Testing – VIPRE for your Home and Business

      Individuals typically marvel, “What’s one of the best antivirus?” A number of distributors will declare that their product is one of the best within...

    Antivirus Testing – VIPRE for your Home and Business

      Individuals typically marvel, “What’s one of the best antivirus?” A number of distributors will declare that their product is one of the best within...

    Related Stories