Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wrote the line below in order to verify if the first or second fields of a list of numbers are equal to 146.

I want to run the line from tcsh, with one line interpreted by bash.

What's the problem here?

echo $numbers
146 146 0 16 16 10 42 12 10 32 32 3 2 32 26

bash -c 'for i in 1 2; do if [ 146 = `echo $numbers | cut -f$i  -d' ' ` ]; then echo "NUM is OK "; fi done'

The errors:

` ]; then echo "NUM is OK "; fi done: -c: line 0: unexpected EOF while looking for matching ``'
` ]; then echo "NUM is OK "; fi done: -c: line 1: syntax error: unexpected end of file
share|improve this question
try "146" == instead of 146 = – saeedn Oct 21 '12 at 13:23
not work ( the same problem ) – yael Oct 21 '12 at 13:24
So the problem is with single quotes you used to wrap your whole commands. It prevents back-quotes to be replaced with its result. Try this: bash -c "for i in 1 2; do if [ 146 = `echo $numbers | cut -f$i -d' ' ` ]; then echo 'NUM is OK '; fi done" – saeedn Oct 21 '12 at 13:32
yes now its works thx – yael Oct 21 '12 at 13:38
Please don't cross-post (superuser.com/questions/490613/linux-write-bash-one-line-syntax), it makes a mess and scatters answers all over the place. Post on one site, the one you think is most appropriate, and the community will move it elsewhere if there is a better home for your question. – Mat Oct 21 '12 at 14:03
up vote 2 down vote accepted

The problem is with single quotes (used to wrap the whole set of commands). The difference between single quotes and double quotes is that in double quotes variables and back-quote commands will be replaced with their value, but in single quotes the string will be treated as is.

You may use:

bash -c "for i in 1 2; do if [ 146 = `echo $numbers | cut -f$i -d' ' ` ]; then echo 'NUM is OK '; fi done"
share|improve this answer

There are a couple problems with your "script", the main one being the ' which would need some form of escaping. Within backticks inside a hard-quoted string this gets hairy.

You could avoid this completely by using $() instead of backticks, and not using hard quotes for the delimiter:

... if [ 146 -eq $(echo $numbers | cut -f$i -d\ ) ]; then ...

Note that -eq is the appropriate predicate for numbers, and that $numbers will need to be exported for this to work at all.

Writing a proper script and calling it from tcsh would be best though.

share|improve this answer

How about grep? Is this a static problem, or will the numbers and criterion change?

If it's static:

echo $numbers | grep -c '(^146\s)|(^\d+\s146\s)'

This returns 0 or 1 depending on if there is a match.

This isn't tested, but it should match first or second field. There are other patterns which may be more efficient, but should be close to working.

share|improve this answer
You can use grep -q to return exit status 0 if a match is found and exit status 1 if it is not found, which integrates better in shell scripts, such as if grep -q "mypattern" myfile; then .... – Daniel Andersson Oct 21 '12 at 15:52
In this case, the question was to find if the first OR the second number was 146. Your pattern only matches if BOTH are 146. Also, you should have a ^ where you have a $. – Daniel Andersson Oct 21 '12 at 15:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.