Home > Enterprise >  Bash command line: Is there a way to use the first parameter's value as part of second paramete
Bash command line: Is there a way to use the first parameter's value as part of second paramete

Time:08-11

I frequently make temporary backups of files by making a file with nearly the same name, e.g.:

cp /some/long/path/code.php /some/long/path/code.phpcode.php.WIP_desc

Is there some way to shorten this without creating an alias?

CodePudding user response:

You can use brace expansion in bash:

cp /some/long/path/code.php{,.WIP_desc}

CodePudding user response:

Create a file named makeFileBackup with this content

#!/usr/bin/env bash
cp "$1" "$1.WIP_desc"

and then run chmod x makeFileBackup.

Now you can use it as /path/to/makeFileBackup some_file.


As suggested in a command, you might want to use the above program without having to specify /path/to/ in front of it. Two general approaches are possible:

  • move makeFileBackup to, or create a link to it in a location that's already in PATH;
  • add to PATH the location where makeFileBackup is; in this case, you probably still don't want it to be in /home/yourusername but in its own directory.

CodePudding user response:

Is creating a variable ok?

p=/some/long/path
cp $p/code.php $p/code.phpcode.php.WIP_desc

Double quote the expansion if p may contain white space.

  • Related