Set command output to variable bash

How To Assign Output of a Linux Command to a Variable

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up. I'm interesting in directing the output from two commands to a variable. I don't want the output to be displayed on the screen, but instead assigned to a variable within my script so that I can use it again. This command gets me the file size in bytes and displays the output as an integer. I am interested in getting this integer assigned to a variable of my liking so that I can compare it against another variable later. Either way I want this numeric output to be assigned to a variable and not displayed on the screen. I prefer the solution offered by Rmano's answerbut if you want to use only redirection:. Ubuntu Community Ask! Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Redirecting output to a variable? Ask Question. Asked 5 years, 4 months ago. Active 1 year, 10 months ago. Viewed 32k times. Like this Any suggestions are very welcomed! Justin Justin 1, 5 5 gold badges 18 18 silver badges 30 30 bronze badges. Active Oldest Votes. It seems quite straightforward. Rmano Rmano The main benefit of using only redirection is that it doesn't swallow errors depending on how you have your script set up for pipe processing - set -eset -o pipefailetc. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Subscribe to RSS


When the user runs any command from the terminal then it shows the output if no error exists otherwise it shows the error message. Sometimes, the output of the command needs to be stored in a variable for future use. Shell command substitution feature of bash can be used for this purpose. How you can store different types of shell commands into the variable using this feature is shown in this tutorial. Bash commands can be used without any option and argument for those commands where these parts are optional. The following two examples show the uses of simple command substitution. The option and argument are mandatory for some bash commands. The following examples show how you can store the output of the command with option and argument into a variable. This command uses -c, -w and -l as option and filename as the argument to generate the output. Create a text file named fruits. Run the following commands to count and store the total number of words in the fruits. Create a text file named weekday. Create a bash file named cmdsub1. In this script, while loop is used to read the content of weekday. You can store the output of command substitution into any loop variable which is shown in the next example. Create a file named cmdsub2. How you can use multiple commands using pipe is shown in the previous example. But you can use nested commands in command substitution where the output of the first command depends on the output of the second command and it works opposite of the pipe command. If you know the path of the command then you can run the command by specifying the command path when using command substitution. The following example shows the use of command path. You can use the command line argument with the command as the argument in the command substitution. Create a bash file named cmdsub3. Various uses of command substitutions are shown in this tutorial. If you need to work with multiple commands or depended commands and store the result temporary to do some other tasks later then you can use this feature in your script to get the output. More info in the video: About the author Fahmida Yesmin I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. Fahmida Yesmin I am a trainer of web programming courses. View all posts.

Subscribe to RSS


By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. It only takes a minute to sign up. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Setting jq output to a Bash Variable Ask Question. Asked 3 years ago. Active 9 days ago. Viewed 34k times. How can I change this so that the username variable gets assigned the output? Active Oldest Votes. I get Syntax error: redirection unexpected with or without the echo as the other answer suggests. In any case, even in zsh or bash which support that operator, that code doesn't make any sense. It redirects the stdin of echo which doesn't read it. It's a wonder how that answer managed to get 10 upvotes. This only works if you lose the "echo" on the second line. Anonymous Anonymous 41 1 1 bronze badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. The Overflow How many jobs can be done at home? Featured on Meta. Community and Moderator guidelines for escalating issues via new response…. Feedback on Q2 Community Roadmap. Related 1. Hot Network Questions. Question feed.

Bash Scripting Tutorial - 2. Variables


For those of you that have dabbled in programming before, you'll be quite familiar with variables. For those of you that haven't, think of a variable as a temporary store for a simple piece of information. These variables can be very useful for allowing us to manage and control the actions of our Bash Script. We'll go through a variety of different ways that variables have their data set and ways we can then use them. Variables are one of those things that are actually quite easy to use but are also quite easy to get yourself into trouble with if you don't properly understand how they work. As such there is a bit of reading in this section but if you take the time to go through and understand it you will be thankful you did later on when we start dabbling in more complex scripts. A variable is a temporary store for a piece of information. There are two actions we may perform for variables:. Variables may have their value set in a few different ways. The most common are to set the value directly and for its value to be set as the result of processing by a command or program. You will see examples of both below. Before Bash interprets or runs every line of our script it first checks to see if any variable names are present. For every variable it has identified, it replaces the variable name with its value. Then it runs that line of code and begins the process again on the next line. Here are a few quick points on syntax. They will be elaborated on and demonstrated as we go into more detail below. Command line arguments are commonly used and easy to work with so they are a good place to start. When we run a program on the command line you would be familiar with supplying arguments after it to control its behaviour. We can do similar with our bash scripts. These are automatically set by the system when we run our script so all we need to do is refer to them.

BASH command output to the variable

Joinsubscribers and get a daily digest of news, geek trivia, and our feature articles. Variables are named symbols that represent either a string or numeric value. When you use them in commands and expressions, they are treated as if you had typed the value they hold instead of the name of the variable. To create a variable, you just provide a name and value for it. Your variable names should be descriptive and remind you of the value they hold. A variable name cannot start with a number, nor can it contain spaces. It can, however, start with an underscore. Apart from that, you can use any mix of upper- and lowercase alphanumeric characters. Giving a variable a value is often referred to as assigning a value to the variable. To see the value held in a variable, use the echo command. The values of the variables replace their names. You can also change the values of variables. So, you can use the same command that references the same variables and get different results if you change the values held in the variables. You can also create a variable that takes its value from an existing variable or number of variables. Scripts would be completely hamstrung without variables. Variables provide the flexibility that makes a script a general, rather than a specific, solution. Type this into a text file, and then save it as fcnt. How can we make the script work with any directory? All it takes is one small change. Many commands, such as ls and wctake command line parameters. These provide information to the command, so it knows what you want it to do. Our scripts can accept command line parameters. You can reference command line parameters in a script just as you would regular variables.

Shell Scripting Tutorial.:8 variable using in Shell Script & Assign Command in variable



Comments on “Set command output to variable bash

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>