0

part of my bash script is

A="-a"
B="|more"
ls $A $B

when executing this script, it complained "|more" not found. How to use pipelines in bash script commands then please?

0

3 Answers 3

2

Maybe like this:

eval "ls $A $B"

?

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

2 Comments

eval is dangerous! What if A='`rm -rf' and B='/`'?
@amphetamachine: Yes, it is dangerous, as is the idea to make a single command out of bits the way the OP wants it.
2

I think what you may be trying for is implementing some sort of variable that controls whether the data is piped through a pager.

You may be better off using alias:

if [ "$USING_PAGER" -ne 0 ]; then
    alias B='more'
else
    alias B='cat'
fi

ls $A |B

1 Comment

Very nice. If the script is for general (public) use (which is probably oftener the case than not), this approach is much safer.
0
ls -a |more

also fails - there is no such command in bash.

3 Comments

actually, most modern bashs are lax enough to allow "|more" without the space.
@cularis: Petar is possibly talking specifically about more, not |more. (Probably because it should be less.)
yeah exactly, sorry, I got distracted :)

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.