Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ksh storing result of a command to a variable

Tags:

linux

command

ksh

I want to store the result of a command to a variable in my shell script. I cant seem to get it to work. I want the most recently dated file in the directory.

PRODUCT= 'ls -t /some/dir/file* | head -1 | xargs -n1 basename'

it wont work

like image 875
user1769538 Avatar asked Dec 21 '22 10:12

user1769538


1 Answers

you have two options, either $ or backsticks`.

1) x=$(ls -t /some/dir/file* | head -1 | xargs -n1 basename)

or

2) x=`ls -t /some/dir/file* | head -1 | xargs -n1 basename`

echo $x

Edit: removing unnecessary bracket for (2).

like image 81
doniyor Avatar answered Jan 13 '23 20:01

doniyor