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
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
The problem that you're having is that the command needs to be surrounded by back-ticks rather than single quotes. This is known as 'Command Substitution'.
Bash allows you to use
$()
for command substitution, but this is not available in all shells. I don't know if it's available in KSH; if it is, it's probably not available in all versions.If the
$()
syntax is available in your version of ksh, you should definitely use it; it's easier to read (back ticks are too easy to confuse with single quotes); back-ticks are also hard to nest.This only addresses one of the problems with your command, however:
ls
returns directories as well as files, so if the most recent thing modified in the specified directory is a sub-directory, that is what you will see.If you only want to see files, I suggest using some version of the following (I'm using Bash, which supports default variables, you'll probably have to play around with the syntax of
$1
)This runs find on the directory, and only pulls files from that directory. It formats all of the files like this:
sorts the list, takes the last line, and chops off everything before the first space.
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`
Edit: removing unnecessary bracket for (2).
You need both quotes to ensure you keep the name even if it contains spaces, and also in case you later want more than 1 file, and "$(..)" to run commands in background
I believe you also need the '-1' option to ls, otherwise you could have several names per lines (you only keep 1 line, but it could be several files)
Please do not put space around the "=" variable assignments (as I saw on other solutions here) , as it's not very compatible as well.
I would do something like:
Your version corrected:
Or simpler:
You want
$()
(preferred) or backticks (``) (older style), rather than single quotes:or