1

I face the problem with $_ usage, which $_ special parameter in terminal, And its call the last argument of the previous command.

It was not work with cp and mv command in gnome-terminal.It happen to me during folder creation and mv or cp file with $_ like below

 mkdir test
 cp file.c $_

instead of copying file to destination , it create a file in current directory named _filedir.

Same happen with mv command

mkdir test
mv file.c $_

instead of moving file to destination, It moving in the name _filedir.

To find the problem i use echo

$ mkdir test
$ echo $_
_filedir

Why is $_ not working with cp, mv commands?

2

1 Answer 1

2

$_ - Gives the last argument to the previous command. At the shell startup, it gives the absolute filename of the shell script being executed. When you execute

mkdir test
mv file.c $_

Check if your mv, cp is an alias

In bash to acces the last arg to the previous command in History use !:$ ,like:

> mkdir test
> mv file.c !:$
mv file.c test
4
  • mv, cp not alias in my bash. I execute it as normal terminal command not a script. By your instrunction mv work with !:$ but not cp. Thank your for this help.
    – SuperKrish
    Nov 11, 2016 at 11:00
  • what's the error that's returned? remember that !:$ will just take the value of the previous command in history last arg; so you can assign it; ex: mkdir test [ENTER] last_param="!:$", so now the variable $last_param will retain the name 'test' even if you execute intermediary commands
    – Arkon-v
    Nov 11, 2016 at 11:13
  • also if you use $_ in a script, so u don't get 'interference' form bash_completion or others, you shouldn't encounter any more troubles like described in your first post
    – Arkon-v
    Nov 11, 2016 at 11:26
  • it not work with cp command, it create the file inthe name of _filedir, similar to making copying a file
    – SuperKrish
    Nov 11, 2016 at 12:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .