-   Linux - General (
-   -   Bash - How do you reference earlier string on line? (

slackert 05-02-2013 12:38 PM

Bash - How do you reference earlier string on line?
I'm looking for something similar to an event designator, but not for history. I'm wondering if it's possible for the current command that isn't in history yet.

Examples using event designators in history:

# cp ~/file /longer/path/name/newfile
# ls -l !cp:2
ls -l /longer/path/name/newfile



# cat /path/to/some/file
# ^cat^rm^
rm /path/to /some/file

What I would like know is if you can do something like:


# cp /path/to/some/file [$foo].new
Where [$foo] references the second "string" in the line which would be "/path/to/some/file". The actual command used here is irrelevant, I know rename of several other equivalents could be used in this example. I've read through the man page for BASH, but I don't even know what to call what I'm looking for.

Anyone have any ideas?

PTrenholme 05-02-2013 02:25 PM

Try # f=/path/to/some/file;cp $f $

I.e., put the part(s) you want to repeat into bash variables and use the variables. There are usually a whole lot of globally defined variables (and scripts) pre-defined by your distribution. Just run set (with no arguments) to see what's in the full list. (On Fedora, I need a set | less since that list includes many global functions.)

All times are GMT -5. The time now is 10:33 AM.