2

I am getting this command not found error in this code. I want to print first word of a sentence

First code (result needed= 1)

abc="1 hello world"; L=$($abc|awk '{print $1}'); echo $L
1: command not found

Second Code (result needed= mp4)

abc="mp4 hello world"; L=$($abc|awk '{print $1}'); echo $L
No command 'mp4' found, did you mean:
 Command 'mpp' from package 'makepp' (universe)
 Command 'mpy' from package 'yorick-mpy-mpich2' (universe)
 Command 'mpy' from package 'yorick-mpy-openmpi' (universe)
 Command 'mpc' from package 'mpc' (universe)
 Command 'm4' from package 'm4' (main)
 Command 'mp4h' from package 'mp4h' (universe)
 Command 'mpv' from package 'mpv' (universe)
 Command 'mpd' from package 'mpd' (universe)
 Command 'mp' from package 'mp' (universe)
 Command 'mpb' from package 'mpb' (universe)
0

5 Answers 5

8

You need echo to pass the variable abc to STDOUT so that awk can use it as STDIN. Although you would get away in this case but always quote the variables (and command substitution) unless you have a very good reason not to (e.g. you want pathname expansion and word spitting to take place):

$ abc="1 hello world"; L="$(echo "$abc" | awk '{print $1}')"; echo "$L"
1

$ abc="mp4 hello world"; L="$(echo "$abc" | awk '{print $1}')"; echo "$L"
mp4

You can also use here strings:

$ abc="1 hello world"; L="$(awk '{print $1}' <<<"$abc")"; echo "$L"
1

$ abc="mp4 hello world"; L="$(awk '{print $1}' <<<"$abc")"; echo "$L"
mp4
0
5

NOTE: it is a good practice to put quotes around the variable you are working with so that it is the only variable you have. Also so that you're not introducing new variables in.

There are some other ways to achieve the same result.

I have some listed below.

abc="1 hello world"; echo "$abc" | awk '{print $1}'

or

abc="mp4 hello world" && echo "$abc" | awk '{print $1}'

or if you want a new variable, according to command substitution, both using backticks "`" and "$(command)" are still valid ways of achieving the results you are after. More information can be found here.

with backtick marks:

abc="mp4 hello world"; L=`echo "$abc" | awk '{print $1}'`; echo "$L"

with "$(command)":

abc="1 hello world"; L="$(echo "$abc" | awk '{print $1}')"; echo "$L"

both will produce a new variable "$L" that is assigned to what you have specified.

2

All you need is to add echo $abc instead of $abc, like so:

abc="1 hello world"; L=$(echo $abc|awk '{print $1}'); echo $L

3
  • Someone removed a point from you because they feel that you need quotes around the $abc, etc. Which is not true. Your command works fine here. I even tested it.
    – Terrance
    Jun 12, 2015 at 13:29
  • @Terrance I have suspended that. It is a good practice to add quotes but in this particular case it's frankly optional Jun 12, 2015 at 14:04
  • That isn't what I meant, and quotes are fine to use. My point was, is your answer works. The other answers here work. I tested yours and it works fine, and I gave you +1. Someone else downvoted you on your answer that works fine. All of this really is optional. I was just letting you know about your downvote is all.
    – Terrance
    Jun 12, 2015 at 14:06
1

The problem with the original code, as has been mentioned, is that it is missing an echo. However, though another poster mentioned that you should always quote variables, this is incorrect. Often quoting a variable will help resolve a possible ambiguous variable name, it often causes other problems by interfering with other quoting. The correct way to specify the variable, in such cases, would be the use of curly brackets around the variable name, not quote around the whole variable. For instance, the original code could be rewritten as: abc="1 hello world"; L=$(echo ${abc}|awk '{print $1}'); echo ${L}

Oh, @heemayl, you forgot to quote the $1 in your example, which is just as well as putting a quote around that variable doesn't work.

3
  • If you want any response from me comment on my answer, if you use "@heemayl" in your answer i won't be notified :) ..now about the $1, the $1 in awk '{print $1}' will be processed by awk not by shell, hence OP has used single quotes around '{print $1}' so that shell does not expand it beforehand ....as a result a double quoting around $1 is redundant here..
    – heemayl
    Jun 12, 2015 at 17:31
  • I actually tried, but couldn't leave a comment on your post. Looks like I need two more points (Want to upvote me so I can? :-) Jun 12, 2015 at 18:09
  • I have upvoted the other answer of yours as this answer raises a direct contradiction with my answer to some extent..
    – heemayl
    Jun 12, 2015 at 18:15
0

In your command ... $($abc|awk '{print $1}'); ..., the content of $abc is executed as command. Therefore the error.

You can test this in a terminal with:

$ 1 your text
1: command not found

Correct your version:

abc="1 hello world"; L=$(echo "$abc"|awk '{print $1}'); echo $L

or use a shorter version:

awk '{print $1}' <<< $(echo "1 your text")

Example

% awk '{print $1}' <<< $(echo "1 hello world")
1

% awk '{print $1}' <<< $(echo "mp4 hello world")        
mp4
3
  • 1
    Someone removed a point from you because they feel that you need quotes around the $abc, etc. Which is not true. Your command works fine here. I even tested it.
    – Terrance
    Jun 12, 2015 at 13:30
  • Downvoters, where is the problem?
    – A.B.
    Jun 12, 2015 at 13:48
  • You got my +1 here as well. I really do like to see all the different ways of achieving the same goal. Sometimes simpler, sometime more complex, but always good to see other ways! Thank you!
    – Terrance
    Jun 12, 2015 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .