LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash - How do you reference earlier string on line? (http://www.linuxquestions.org/questions/linux-general-1/bash-how-do-you-reference-earlier-string-on-line-4175460465/)

slackert 05-02-2013 01: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:
Code:

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

or

Code:

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

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

Code:

# 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 03:25 PM

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

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 09:04 AM.