LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 05-02-2013, 01:38 PM   #1
LQ Newbie
Registered: Dec 2007
Posts: 6

Rep: Reputation: 1
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?
Old 05-02-2013, 03:25 PM   #2
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,170

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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.)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [bash - sed - awk] Match line with x characters and add string TigerClaw Linux - Newbie 4 02-28-2012 01:22 AM
[SOLVED] BASH (grep / sed / awk): find string within a (line) range dragonetti Linux - Newbie 2 11-24-2011 11:16 PM
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 10:38 AM
bash: append string to end of line khairil Programming 6 02-27-2007 06:09 AM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 06:43 PM

All times are GMT -5. The time now is 05:02 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration