How to get the value of the output of ls/dir command in a Linux shell console?

I need to to this operation automaticaly:

$ ls
this_folder
$ cp -rf this_folder/* .

For this I need to store in a variable the value of the "ls" so that I do something like this:

$ ls
this_folder
$ cp -rf $value_of_the_ls/* .

It is possible to do this? Give me some clues.

Best Regards,

2

1 Answer

To set the output of a command into a variable, use command substitution like this:

$ value_of_the_ls=$(ls)
$ echo "${value_of_the_ls}"

Have you thought about what you will do if there are multiple files in the current directory in which case ls will return multiple files?

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like