0

I need a Shell Script that looks at the specified directory and assigns the name of the last modified subdirectory to a specific variable.

I can get the name of the latest subdirectory in /apps/ like this:

cd /apps
ls -td -- */ | head -n 1 | cut -d'/' -f1

But how do I assign the result of the second command to a variable, so I can use the parameter in the further script commands (e.g. mkdir $variable)?

0

1 Answer 1

2

What you're asking for is called command substitution:

cd /apps
some_dir=$(ls -td -- */ | head -n 1 | cut -d'/' -f1)
mkdir -p "$some_dir"
Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic, exactly what I needed.
This assigns to a variable, not to a parameter. If this is what is desired, the question should be edited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.