2

I am trying to pass pound sign as a command line argument then echoing the argument. When I pass for example 2 # 2 then I echo $1 $2 $3, I assume the terminal will output 2 # 2 but it outputs just 2. What am I missing ?

4

# is the comment character. If you don't want it to be, you need to either escape it, or quote it.

echo 2 \# 2
echo 2 '#' 2
6
  • I have an assignment in which it says when the command is for example 2 # 2, the output should be "Error". So I shouldn't write a backslash before the pound sign or wrap it around quotes. Just #.
    – Altaïr
    Apr 14 '15 at 1:30
  • @Altaïr I'm afraid I do not understand what you are saying, or how it relates to your question.
    – Chris Down
    Apr 14 '15 at 1:35
  • 1
    There is no way to write a program that can distinguish between prog 2 # 2 and prog 2.
    – Scott
    Apr 14 '15 at 1:42
  • 1
    However, if there is no space before the #. it is not considered a comment. So prog 2#2 has one argument, which is 2#2.
    – rici
    Apr 14 '15 at 1:56
  • Also, in an interactive shell, the # is only a comment if the interactive_comments option is set
    – chepner
    Apr 14 '15 at 2:13
1

It is not possible if you use bash as your shell (but as said above you can escape it, or you can put it in quotation marks).

However it will work fine if you launch your program from csh for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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