Echo 'The Character - (Dash) in The Unix Command Line

echo 'the character - (dash) in the unix command line

This question was asked more than 2 years ago, but I've just noticed that echo on bash and zsh behaves differently.

On Zsh, the first dash is used to terminate option processing; so as @asatsi answered, you can print - with

% echo - -
-

On Bash, single dash works just fine.

$ echo -
-

From The Z Shell Manual (http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html)

echo [ -neE ] [ arg ... ]

...

Note that for standards compliance a double dash does not terminate option processing; instead, it is printed directly. However, a single dash does terminate option processing, so the first dash, possibly following options, is not printed, but everything following it is printed as an argument. The single dash behaviour is different from other shells. For a more portable way of printing text, see printf, and for a more controllable way of printing text within zsh, see print.

How to repeat a dash (hyphen) in shell

This throws an error:

$ printf '-%.0s' {1..100}; echo ""
bash: printf: -%: invalid option
printf: usage: printf [-v var] format [arguments]

This works fine under bash:

$ printf -- '-%.0s' {1..100}; echo ""
----------------------------------------------------------------------------------------------------

For other shells, try:

printf -- '-%.0s' $(seq 100); echo ""

The problem was the printf expects that - starts an option. As is common among Unix/POSIX utilities in this type of situation, -- signals to printf to expect no more options.

Bash: echo string that starts with -

The variable VAR contains -e xyz, if you access the variable via $ the -e is interpreted as a command-line option for echo. Note that the content of $VAR is not wrapped into "" automatically.

Use echo "$VAR" to fix your problem.

What's the magic of - (a dash) in command-line parameters?

If you mean the naked - at the end of the tar command, that's common on many commands that want to use a file.

It allows you to specify standard input or output rather than an actual file name.

That's the case for your first and third example. For example, the cdrecord command is taking standard input (the ISO image stream produced by mkisofs) and writing it directly to /dev/dvdrw.

With the cd command, every time you change directory, it stores the directory you came from. If you do cd with the special - "directory name", it uses that remembered directory instead of a real one. You can easily switch between two directories quite quickly by using that.

Other commands may treat - as a different special value.

How to reassign a variable (zsh) when using source utility

Your code gives the expected output for bash 4.2.46 on RHEL7.
Are you maybe using zsh?

See echo 'the character - (dash) in the unix command line

EDIT: Ok, if it's zsh, you probably have to use a hack:

if [[ ${output} == '-' ]]; then
echo - ${output}
else
echo ${output}
fi

or use printf:

printf  $output"\n"

How can I grep for a string that begins with a dash/hyphen?

Use:

grep -- -X

Documentation

Related: What does a bare double dash mean? (thanks to nutty about natty).

How can I parse/capture strings separated by dashes?

Use the built-in read command:

str='string1-string2-string3-string4-etc'
IFS=- read str1 str2 the_rest <<< "$str"

unix echo string with underscore next to variable

printf works in this case (since $var_name stands alone):

$ var_name="foo"
$ printf "%s_bar\n" "$var_name"
foo_bar

Or use {braces} around the variable to disambiguate where the variable name ends within an interpolated string:

$ echo "${var_name}_bar"
foo_bar

How to echo bare hypen (`-`) in zsh?

Try echo - "-". The first dash terminates option processing, so following text is printed.

See this excellent answer for more context: https://stackoverflow.com/a/57656708/11776945



Related Topics



Leave a reply



Submit