Linked Questions
52 questions linked to/from How can I assign the output of a command to a shell variable?
355
votes
5
answers
1.4m
views
How can I get the current working directory? [duplicate]
I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd. It just prints pwd how do I get the current working ...
31
votes
4
answers
144k
views
Storing output of command in shell variable [duplicate]
I have an operation using cut that I would like to assign result to a variable
var4=echo ztemp.xml |cut -f1 -d '.'
I get the error:
ztemp.xml is not a command
The value of var4 never gets assigned; ...
12
votes
5
answers
7k
views
Why is my Bash script returning a syntax error? [duplicate]
I am writing a script which loops into recent files in a folder and executes a command...
#!/bin/bash
cd /home/Downloads
recent_files = ($(ls -t | head -20))
for file in "${recent_files[@]}"
do
....
0
votes
2
answers
30k
views
curl command output in a variable [duplicate]
I am connecting to mySQL database using a shell script. After connecting to the mySQL database, I execute a query. The query gives me 300,000 URLs as the result.
For each of the 300,000 URLs I need ...
1
vote
1
answer
16k
views
wc -l file to a variable [duplicate]
Given a filepath, I would like to find out how many lines there are and store them into a variable. For example:
/three_little_pigs.csv
straw
wood
bricks
In a file, I would like to have the number (...
1
vote
1
answer
28k
views
How to read the output of a previous command in a shell script? [duplicate]
I executed a command in a shell script and the command ends with "completed successfully" or "completed unsuccessfully". How can I read that in the script and use it for if and else conditions?
0
votes
1
answer
10k
views
How can I get the result of ls into a variable in bash Script? [duplicate]
I'm a novice at bash (more like less than a novice). Trying to get the results of this into a variable
ssh root@HOST "ls -tr /path/to/files/Backups | tail -1"
This is what I have
#!/bin/bash
STR=ssh ...
0
votes
1
answer
10k
views
How to use pipe to assign a variable [duplicate]
How can I assign value of $x1+$x2 to bc by piping? here in my code $bc holds no value.
echo -n 'value x1 : '
read x1
echo -n 'value x2 : '
read x2
echo $x1+$x2 | bc
echo $bc
1
vote
2
answers
4k
views
How to assign value using echo to a variable but don't display it in terminal [duplicate]
My bash script
#!/bin/bash
read -r -p "Enter the filenames: " -a arr
for i in "${arr[@]}"
do
echo $i | sed 's/\(.*\)\..*/\1/'
cp -v ~/Desktop/library/template.py "$i&...
1
vote
1
answer
4k
views
How do I set a bash variable that contains another variable? [duplicate]
I am trying to set the variable service to be the value of
var1=first/second
echo $var1 | cut -d '/' -f 1
var2=$var1 | cut -d '/' -f 1"
echo $var2
The result of echo $var1 | cut -d '/' -f 1 is "...
0
votes
1
answer
5k
views
How capture remote ssh output into a variable [duplicate]
I would like to know how to capture the output of this command into a variable
ssh -q $ssh_host 'ps -eo comm,lastcpu'
Once the information is captured, I want to check the variable if it returned ...
0
votes
1
answer
4k
views
syntax for using head with filename stored in variable [duplicate]
I'm trying to store the first 5 lines from a file in the variable url. But I get an error
-n5: command not found
fn_all_urls stores the path to the file /home/urls.txt
My Line is:
url=head -n5 ${...
0
votes
1
answer
3k
views
Assigning stdout value to a variable [duplicate]
So I'm trying to make a sh script where I put a value that I get from a command, into a variable, in this case n, but I have no idea what I'm doing, and this here obviously doesn't work lol:
1 n=0
...
0
votes
1
answer
2k
views
want to store date output in variable [duplicate]
i tried the below code to store the date output in variable.
read -p 'date: ' mydate
date_month= date -d "$mydate" +%b
echo $date_month
it is not printing the output.
how to store the date output in ...
1
vote
1
answer
2k
views
set a BASH variable equal to the output from 'top' command? [duplicate]
I know how to set a BASH variable equal to the output from a command, but it doesn't work in case of 'top' command for me, see below
cat aTOPFWK.sh
OUTPUT = $(top -b -n 1 | egrep 'fwk3' | awk '{...