igniteflow
igniteflow

Reputation: 9802

Why does tr -d replace the target with -?

According to the tr man:

-d, --delete delete characters in SET1, do not translate

However

$ echo "abc" | tr "a" -d 
-bc

Now this could be removed with an additional command such as

echo "abc" | tr "a" -d | sed 's/-//'

but am I missing something here? Why does delete do a replace with - ?

Upvotes: 1

Views: 381

Answers (2)

vidit
vidit

Reputation: 6461

You are doing it wrong. You pass the option before SET1, as manual says tr [-Ccu] -d string1. So your command should be echo "abc" | tr -d "a".

The command tr "a" -d replaces all a with -. Try the following commands, you'll figure out what's going on better..

echo "abc" | tr "a" de

echo "abc" | tr "a" +e

Upvotes: 2

Celada
Celada

Reputation: 22261

Your options and arguments are in the wrong order in:

tr "a" -d

Standard command line syntax is that options are provided before all non-option arguments, like this:

tr -d a

Under this syntax, the first non-option argument (or the explicit argument --) terminates option processing, and everything else after that on the command line is an argument.

A few commands like gcc traditionally break this convention and mix options with arguments. In this case, it is usually because the ordering of options and arguments is significant (typically, options apply only to the arguments that follow them but not to the arguments that precede them).

Also, some commands relax the rule and allow options to be mingled with arguments in every case, but this is non-standard and should not generally be used, especially if you want code to be as compatible/portable as possible.

Upvotes: 1

Related Questions