1

I want to do this

mv <some path> <some path>/../

is there a way to pass argv[1] as my argument and manipulate it, instead of writing the whole path again? i.e

mv <some path> argv[1]/../
2

2 Answers 2

5

I've found the answer here. You can do it with:

mv file.txt !#:1.backup
  • !# - refers to the current command.
  • !#:1 - refers to the first argument of the current command

If you run this example you'll get:

mv file.txt file.txt.backup
1
  • 2
    Notice that history expansion (which this is a part of) is disabled for non-interactive shells by default, so this wouldn't work in a script without explicitly enabling it first. Sep 13, 2020 at 21:11
2

In these situations, I usually use brace expansion:

mv <some path>{,/../}

which will expand to

mv <some path> <some path>/../

Example:

$ echo mv some/path{,/../other/path}
mv some/path some/path/../other/path

As @Pavlo Myroniuk points out in his answer, you can also follow the advice from here and do:

mv <some path> !#:1/../
0

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.