0

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.

5
  • 2
    Pipes involve sub-shells. The pipe there is also useless and incorrect. Drop the pipe. Commented Jan 13, 2015 at 15:30
  • @user2610 please explain what you want to achieve. Commented Jan 13, 2015 at 15:37
  • @chaos : i want to store the output of echo to a variable and then segregate that string into different substrings. Commented Jan 13, 2015 at 15:40
  • @EtanReisner:I want to store the variable to "a" and use it further in my command line as shown. Commented Jan 13, 2015 at 15:45
  • What's wrong with simply cat time? Why do you have to capture the output of cat just to echo it again? Also, echo is not likely to read from stdin, so ... | echo ... is a bit pointless. Commented Jan 13, 2015 at 16:56

2 Answers 2

1
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:

echo $a

That prints: 19991213100942

echo ${a:0:4}

That prints: 1999

You can reference the varibale by its name $a.

Sign up to request clarification or add additional context in comments.

2 Comments

I want to know why the command line is not working as a whole: a=$(echo cat time) | echo $a | echo ${a:0:4}. It does not give any output.
You have to use semicolons, bot pipes: a=$(echo cat time); echo $a; echo ${a:0:4}
1

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}

Comments

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.