LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-02-2013, 12:38 PM   #1
slackert
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:
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?
 
Old 05-02-2013, 02:25 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

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


Reply



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 12:22 AM
[SOLVED] BASH (grep / sed / awk): find string within a (line) range dragonetti Linux - Newbie 2 11-24-2011 10:16 PM
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 05:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:07 AM.

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