Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
The below code does not give any output:
$echo `cat time` 19991213100942 $a=$(echo `cat time`) | echo $a | echo ${a:0:4}
Please tell where I am making mistake.
cat time
cat
echo
... | echo ...
a=$(echo `cat time`)
assigns the output of the command inside the brackets $(...) to the variable $a. Later in the script, you can print the variable:
$(...)
$a
echo $a
That prints: 19991213100942
19991213100942
echo ${a:0:4}
That prints: 1999
1999
You can reference the varibale by its name $a.
Add a comment
a=$(echo cat time); echo $a; echo ${a:0:4}
First, you don't need to echo the output of cat time: just cat time.
Second, as @Etan says (kind of), replace the pipes with semicolons or newlines
a=$(< time) # a bash builtin, equivalent to but faster than: a=$(cat time) echo $a echo ${a:0:4}
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
cat time? Why do you have to capture the output ofcatjust toechoit again? Also,echois not likely to read from stdin, so... | echo ...is a bit pointless.