I’m trying to create a simple for loop that will take all the outputs of an ls command and put each output into a variable. so far i have
for i in ls /home/svn
do
echo $i
done
but it gives me an error.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because the
lsneeds to be executed:Also, you might want to consider shell globbing instead:
… or
find, which allows very fine-grained selection of the properties of items to find:Furthermore, if you can have white space in the segments of the path or the file names themselves, use a while loop (previous example adjusted):
whilereads line-wise, so that the white space is preserved.Concerning the calling of
basenameyou have two options:To explain the substitution:
##removes the longest front-anchored pattern from the string,#up to the first pattern match,%%the longest back-anchored pattern and%the first back-anchored full match.