Linux set variable from command

Setting environment variables in Linux using Bash

What is the equivalent to the tcsh setenv function in Bash? Is there a direct analog? The environment variables are for locating the executable.

5 Answers 5

export VAR=value will set VAR to value. Enclose it in single quotes if you want spaces, like export VAR=’my val’ . If you want the variable to be interpolated, use double quotes, like export VAR=»$MY_OTHER_VAR» .

PS: no need for the double quotes in the last one. The shell does not perform word splitting for variable assignments.

Just for the beginners. I had not understood it directly, here in normal English: interpolate «$X» means here that you already have a variable X that is filled with a value, e.g. «A», and you want to read out that value «A» first, and then assign this value as the value of VAR. You do not want VAR to be a string of «$X» here of course. And then the comment above seems also logical, that you will not even need the «». And for a direct assignment of «A», use ‘A’. Please correct me if I got it wrong.

The reason people often suggest writing

is that the longer form works in more different shells than the short form. If you know you’re dealing with bash , either works fine, of course.

Set a local and environment variable using Bash on Linux

Check for a local or environment variables for a variable called LOL in Bash:

el@server /home/el $ set | grep LOL el@server /home/el $ el@server /home/el $ env | grep LOL el@server /home/el $ 

Sanity check, no local or environment variable called LOL.

Set a local variable called LOL in local, but not environment. So set it:

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ env | grep LOL el@server /home/el $ 

Variable ‘LOL’ exists in local variables, but not environment variables. LOL will disappear if you restart the terminal, logout/login or run exec bash .

Set a local variable, and then clear out all local variables in Bash

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ exec bash el@server /home/el $ set | grep LOL el@server /home/el $ 

You could also just unset the one variable:

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ unset LOL el@server /home/el $ set | grep LOL el@server /home/el $ 

Local variable LOL is gone.

Promote a local variable to an environment variable:

el@server /home/el $ DOGE="such variable" el@server /home/el $ export DOGE el@server /home/el $ set | grep DOGE DOGE='such variable' el@server /home/el $ env | grep DOGE DOGE=such variable 

Note that exporting makes it show up as both a local variable and an environment variable.

Читайте также:  Wifi setup linux mint

Exported variable DOGE above survives a Bash reset:

el@server /home/el $ exec bash el@server /home/el $ env | grep DOGE DOGE=such variable el@server /home/el $ set | grep DOGE DOGE='such variable' 

Unset all environment variables:

You have to pull out a can of Chuck Norris to reset all environment variables without a logout/login:

el@server /home/el $ export CAN="chuck norris" el@server /home/el $ env | grep CAN CAN=chuck norris el@server /home/el $ set | grep CAN CAN='chuck norris' el@server /home/el $ env -i bash el@server /home/el $ set | grep CAN el@server /home/el $ env | grep CAN 

You created an environment variable, and then reset the terminal to get rid of them.

Or you could set and unset an environment variable manually like this:

el@server /home/el $ export FOO="bar" el@server /home/el $ env | grep FOO FOO=bar el@server /home/el $ unset FOO el@server /home/el $ env | grep FOO el@server /home/el $ 

Источник

bash set variable from command

To set an environment variable everytime, use the export command in the . bashrc file (or the appropriate initialization file for your shell). To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.

How do you assign a command to a variable in a shell script?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option . ] arg1 arg2 . ) OR variable_name=’command’ variable_name=’command [option . ] arg1 arg2 . ‘

How do you set a variable in terminal?

Setting temporary environment variable using terminal

You can do this simply by opening the terminal and running export command followed by the variable name and its value. the ‘:’ here append the value to the existing value. See example below.

WHAT IS SET command in bash?

This new Shell is the execution environment of the script, and different parameters of the environment can be given in Bash by default. The set command is used to modify the operating parameters of the Shell environment, which means that the environment can be customized.

How do you set a variable in Linux?

  1. Open the current user’s profile into a text editor. vi ~/.bash_profile.
  2. Add the export command for every environment variable you want to persist. export JAVA_HOME=/opt/openjdk11.
  3. Save your changes.

How do I set environment variables?

  1. From the desktop, right click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables. .
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable.

How do you set a variable in UNIX?

If what you want is for the variable to be available to every session, instead of just the current one, you will need to set it in your shell run control. Then add the set line or the setenv line shown above to automatically set the variable or environment variable for every session of csh.

What is the output of who command?

The basic syntax for using who command is as follows. 1. If you run who command without any arguments, it will display account information (user login name, user’s terminal, time of login as well as the host the user is logged in from) on your system similar to the one shown in the following output.

Читайте также:  Linux compile python to executable

How do you check if a variable contains a string in bash?

The easiest approach is to surround the substring with asterisk wildcard symbols (asterisk) * and compare it with the string. Wildcard is a symbol used to represent zero, one or more characters. If the test returns true , the substring is contained in the string.

How do I see environment variables in terminal?

Enter echo $VARIABLE. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo $MARI_CACHE. If the variable is set, its value is displayed in the terminal window.

Which command displays all environment variables?

Summary:

Command Description
echo $VARIABLE To display value of a variable
env Displays all environment variables
VARIABLE_NAME= variable_value Create a new variable
unset Remove a variable

How do I export a variable in Linux?

  1. vech=Bus. Display the value of a variable with echo, enter:
  2. echo «$vech» Now, start a new shell instance, enter:
  3. bash. .
  4. echo $vech. .
  5. export backup=»/nas10/mysql» echo «Backup dir $backup» bash echo «Backup dir $backup» .
  6. export -p.

How To Install And Use MySQL Workbench On Ubuntu

Mysql

Installing MySQL WorkbenchStep 1: Download configuration file from the apt repository. Using this method, you can install MySQL from the official apt.

How to Install Slack on Debian 10 Linux

Slack

Installing Slack on DebianDownload Slack. Open to the Slack for Linux download page in your browser and download the latest Slack . . Install Slack.

Changing the MAC Address Changer

Address

Right-click or long tap on the adapter for the network card that you want to change. Choose Properties from the menu that opens up. In the Properties .

Latest news, practical advice, detailed reviews and guides. We have everything about the Linux operating system

Источник

How can I assign the output of a command to a shell variable?

I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it. Here’s what I’ve got:

#!/bin/bash cd ~/Desktop; thefile= ls -t -U | grep -m 1 "Screen Shot"; echo "Most recent screenshot is: "$thefile; 
Screen Shot 2011-07-03 at 1.55.43 PM.png Most recent screenshot is: 

So, it looks like that isn’t getting assigned to $thefile , and is being printed as it’s executed. What am I missing?

3 Answers 3

A shell assignment is a single word, with no space after the equal sign. So what you wrote assigns an empty value to thefile ; furthermore, since the assignment is grouped with a command, it makes thefile an environment variable and the assignment is local to that particular command, i.e. only the call to ls sees the assigned value.

You want to capture the output of a command, so you need to use command substitution:

thefile=$(ls -t -U | grep -m 1 "Screen Shot") 

(Some literature shows an alternate syntax thefile=`ls …` ; the backquote syntax is equivalent to the dollar-parentheses syntax except that quoting inside backquotes is weird sometimes, so just use $(…) .)

Other remarks about your script:

  • Combining -t (sort by time) with -U (don’t sort with GNU ls ) doesn’t make sense; just use -t .
  • Rather than using grep to match screenshots, it’s clearer to pass a wildcard to ls and use head to capture the first file:
 thefile=$(ls -td -- *"Screen Shot"* | head -n 1) 
 echo "Most recent screenshot is: $thefile" 

If you have GNU find and sort (in particular if you’re running non-embedded Linux or Cygwin), there’s another approach to finding the most recent file: have find list the files and their dates, and use sort and read (here assuming bash or zsh for -d » to read a NUL-delimited record) to extract the youngest file.

IFS=/ read -rd '' ignored thefile < <( find -maxdepth 1 -type f -name "*Screen Shot*" -printf "%T@/%p\0" | sort -rnz) 

If you're willing to write this script in zsh instead of bash, there's a much easier way to catch the newest file, because zsh has glob qualifiers that permit wildcard matches not only on names but also on file metadata. The (om[1]) part after the pattern is the glob qualifiers; om sorts matches by increasing age (i.e. by modification time, newest first) and [1] extracts the first match only. The whole match needs to be in parentheses because it's technically an array, since globbing returns a list of files, even if the [1] means that in this particular case the list contains (at most) one file.

#!/bin/zsh set -e cd ~/Desktop thefile=(*"Screen Shot"*(om[1])) print -r "Most recent screenshot is: $thefile" 

Источник

How To Assign Output of a Linux Command to a Variable

When you run a command, it produces some kind of output: either the result of a program is suppose to produce or status/error messages of the program execution details. Sometimes, you may want to store the output of a command in a variable to be used in a later operation.

In this post, we will review the different ways of assigning the output of a shell command to a variable, specifically useful for shell scripting purpose.

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below:

variable_name=$(command) variable_name=$(command [option . ] arg1 arg2 . ) OR variable_name='command' variable_name='command [option . ] arg1 arg2 . '

Below are a few examples of using command substitution.

In this first example, we will store the value of who (which shows who is logged on the system) command in the variable CURRENT_USERS user:

Then we can use the variable in a sentence displayed using the echo command like so:

$ echo -e "The following users are logged on the system:\n\n $CURRENT_USERS"

In the command above: the flag -e means interpret any escape sequences ( such as \n for newline) used. To avoid wasting time as well as memory, simply perform the command substitution within the echo command as follows:

$ echo -e "The following users are logged on the system:\n\n $(who)"

Shows Current Logged Users in Linux

Next, to demonstrate the concept using the second form; we can store the total number of files in the current working directory in a variable called FILES and echo it later as follows:

$ FILES=`sudo find . -type f -print | wc -l` $ echo "There are $FILES in the current working directory."

Show Number of Files in Directory

That’s it for now, in this article, we explained the methods of assigning the output of a shell command to a variable. You can add your thoughts to this post via the feedback section below.

Источник

Оцените статью
Adblock
detector