Adalid_Negro
Adalid_Negro

Reputation: 53

What is the meaning of !#:* !#:1- in a bash command?

In the following Bash command, what is the meaning of: !#:* !#:1

echo "This is a sentence." !#:* !#:1- >text3

Upvotes: 4

Views: 1314

Answers (2)

Mark Reed
Mark Reed

Reputation: 95252

It's using bash's history substitution mechanism.

Specifically, !# refers to the current line (up to but not including the location of the !# itself). !#:* is the part of that line after the command name (so, in this case, "This is a sentence."). !#:1- is the same as !#:* except that it omits the last word (so it doesn't include the second copy of "This is a sentence" that we just added via the !#:*).

The end result is a line with three copies of This is a sentence. echoed into a file named text3.

Upvotes: 8

RahulKrishnan R A
RahulKrishnan R A

Reputation: 566

The output from:

echo "hello" !#        

is equivalent to the output from:

echo "hello" echo "hello"

which is:

hello echo hello

!# means substitute previous string before !# again to current line (shortcut to avoid writing again)

     0th    1st     2nd      3rd
-------- ------- ------ --------
    echo "hello"   echo  "hello"
-------- ------- ------  -------

!#:0 means substitute value in 0th col

!#:1 means substitute value in 1st col

Example

echo "hello" !#:1

The output from that is the same as the output from:

echo "hello" "hello"

which is:

hello hello

!#:1 is replaced by string in 1st column — "hello"

echo "hello" !#:0

produces the same output as:

echo "hello" echo

which is:

hello echo

!#:0 is replaced by string in 0th column — echo

Upvotes: 7

Related Questions