2

I am facing some minor problems when coding linux shell scripts.

My codes are below and I am struggling to input my modified string names to the variable named "fileName" as shown in below. And then, I commanded echo to identify the previous code is working well. However, it outputs nothing.

How to resolve this issues?

for file in ./*.txt
do
fileName=$file | rev  | cut -d. -f2 | rev
     echo {$fileName}
done

As I said, echo{$fileName} didn't output the words that I wanted to extract.

I look forward to your helpful advice :)

1
  • To assign a modified value to fileName, you need something like fileName=$(echo "$file" | rev | cut -d. -f2 | rev). This echoes the value in $file to the sequence of commands and captures the output in the variable. In Bash, you could use a here string instead of echo. Commented Dec 15, 2015 at 4:33

1 Answer 1

1

You need to use command substitution $():

Also use echo to get the value of variable file:

fileName="$( echo "$file" | rev  | cut -d. -f2 | rev )"

Or use Here strings:

fileName="$( rev <<<"$file" | cut -d. -f2 | rev )"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer.
Okay. I will do it. However, when I used my coded with awk, it didn't work.. I ran the code below in the for loop. awk '{print($2"\t"$3"\t"$4"\t""0""\t""0""\t"$5"\t"$6"\t"$7"\t"$8"\t"$fileName)}' $file

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.