Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the current user to a variable

Tags:

bash

I want 'me' to equal $whoami , but when I try to do it I just print "whoami" as a string instead of the actual value. Would it be possible to use a pipe to connect the variable and the command?

like image 477
Lukasz Medza Avatar asked Nov 26 '13 15:11

Lukasz Medza


People also ask

How do you store Whoami in a variable?

It must get the user's name using the whoami command and store it in a variable called username . It must take a single parameter which is the name of the file to be searched. It must grep to search the specified file for occurrences of the user's name and print them.

How do I save terminal output to a variable?

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 ...]

How do you use variables in terminal?

Using variable from command line or terminal You don't have to use any special character before the variable name at the time of setting value in BASH like other programming languages. But you have to use '$' symbol before the variable name when you want to read data from the variable.


2 Answers

Use it like this:

me="$(whoami)"

to store command whoami output to shell variable me

like image 68
anubhava Avatar answered Nov 15 '22 08:11

anubhava


what about:

me="$USER"

but why should you do this ? It is already an environment variable :-)

echo "$USER"
like image 36
thom Avatar answered Nov 15 '22 07:11

thom