repeating previous argument on command line?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers repeating previous argument on command line?
# 1  
Old 07-30-2005
repeating previous argument on command line?

Hi, is there a way in bash--or any other shell--to repeat the preceding argument on the command line? E.g., let's say I want to rename the file "/var/www/conf/httpd.conf" to "/var/www/conf/httpd.conf.bak". I want to be able to type mv /var/www/conf/httpd.conf, and then press a command key that would repeat the preceding argument, so I get mv /var/www/conf/httpd.conf /var/www/conf/httpd.conf, and then I can simply tack on ".bak" on the final argument so generated.
thanks,
# 2  
Old 07-30-2005
I use ksh and vi-mode command line editing. vi-mode command line editing is available on bash too (with set -o vi).

I'd do something similar to the following..... (I'm emphasising the <space>'s here to show what I'm doing....
Code:
mv<space>/var/www/conf/httpd.conf<space>
...then...
<Esc>F<space>ly$$pi.bak

Probably just as easy to retype it Smilie

That's...
<Esc> - Enter command mode
F<space> - Go backwards to the first space (before /var)
l - Go right one character
y$ - Yank to end of line
$ - Go to end of line
p - Paste
i - insert

Then, we're back in editing mode to add .bak

All done Smilie

Might be an easier way with bash....?

Cheers
ZB
# 3  
Old 07-30-2005
I use emacs mode with ksh and bash. In either shell, I can use the sequence <escape> <dot> to represent the last argument on the previous command. But this only works in emacs mode.
set -o emacs
# 4  
Old 07-30-2005
I do a similar thing in tcsh with !$ to repeat the last argument of the previous command.

For the previous argument of the current command, there is no easy way that I see of doing this.... the quickest route would probably be....

Code:
x=/var/www/conf/httpd.conf
mv $x $x.bak

Cheers
ZB
# 5  
Old 07-30-2005
I missed that the OP wanted to repeat the last arg on the current line. I'll confess that I use a somewhat lame trick in a case like that...

echo some-really-long-argument
cp <escape><dot> <escape><dot>.old
# 6  
Old 07-30-2005
BINGO: How to repeat previous argument on shell command line

I found out that there is currently no readline function (bash uses the readline library for command line editing) to repeat the previous argument. However, I did solve the problem:

As a general solution, for any shell that supports brace expansion, you can use this trick (note that csh, tcsh, bash and pdksh support brace expansion, whereas sh and ksh do not):
mv /var/www/conf/httpd.conf{,.bak}

I.e., the first element in the brace is simply empty. This trick does what we want, but the arguments will not be visible as two separate arguments on the command line, but will be separated (as mv /var/www/conf/httpd.conf /var/www/conf/httpd.conf.bak) when the command is actually executed.

If you actually want an editing command that will repeat the previous argument on the command line, you have to create a macro.

If you use pdksh in emacs mode, you can put the following line in its ENV config file:
bind -m '^[w=^[b^[^] ^k^y^y'

(Note there are spaces in here.) This creates a macro bound to Meta-w (Alt-w).

If you use bash in emacs mode (or use any other readline-enabled program), then create a file ~/.inputrc, and put the following line there:

Meta-w: "\C-w\C-y\ \C-y"

(Note there is a space b/w two slashes here.) This creates a macro bound to M-w (Alt-w) that cuts/kills the previous non-whitespace string (C-w), and then pastes/yanks (C-y) it two times, putting a space in between. This does just what we want. (If you want to try it out for the current session, before making it permanent in .inputrc, type bind 'Meta-w: "\C-w\C-y\ \C-y"' at the bash command line.)

cheers... Smilie

Last edited by hadarot; 08-01-2005 at 11:15 PM..
# 7  
Old 08-01-2005
deleted - misread OPs intentions

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add command line argument

I would like to add the ability to change the message that is displayed when timer is finished. At present it just asks for the time I want for the alarm. I think what I need is another command line argument. soundfile="/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3"... (5 Replies)
Discussion started by: drew77
5 Replies

2. UNIX for Beginners Questions & Answers

Command line argument

Hi Guys, I'm trying to work out how to add a command line argument inside single quotes. Would anyone be able to help please as I'm going mad :) I want to be able to place the filename on command line and it then be used in a script but it needs to have quotes surrounding it. Thanks in... (4 Replies)
Discussion started by: mutley2202
4 Replies

3. Shell Programming and Scripting

Specify an entire UNIX command as a command line argument

I'm trying to write a bash script called YN that looks like the following YN "Specify a question" "doThis" "doThat" where "doThis" will be executed if the answer is "y", otherwise "doThat". For example YN "Do you want to list the file dog?" "ls -al dog" "" Here's my attempt... (3 Replies)
Discussion started by: LeoKSimon
3 Replies

4. Shell Programming and Scripting

Can a string be a command line argument?

I would like to use a string as a command line argument...is this possible using TCSH? For example say my script is called TEST and I would like to pass a string into my script stating why the test failed. EXAMPLE: TEST "Failed due to missing statement" (4 Replies)
Discussion started by: thibodc
4 Replies

5. Shell Programming and Scripting

command line argument - perl

how do i check if a command line argument is -g? for example, if command line argument equals "-g" { print "Goodbye \n"; } else { print "Welcome to the program! \n"; } (1 Reply)
Discussion started by: bshell_1214
1 Replies

6. Shell Programming and Scripting

command-line line 0: Missing yes/no argument

Hi Guys When I run the below command ssh -o 'PasswordAuthentication yes' -o 'PreferredAuthentications publickey' -i $HOME/.ssh/id_dsa Server_Name I found the below error ommand-line line 0: Missing yes/no argument Kindly help me to sort out Double post, continued... (0 Replies)
Discussion started by: Pratik4891
0 Replies

7. Programming

Command Line Argument

Hi, I have a very simple C program which will run in UNIX. When i am passing * as the command line argument, i am gettig the below output. Program: #include <stdio.h> #include "mylibrary.h" int **environ; int main(int argc,char *argv) { int i; printf("\nHello... (2 Replies)
Discussion started by: dsudipta
2 Replies

8. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

9. Shell Programming and Scripting

How to get the value in last command line argument???

Say I want to get the value of last command line argument using the value in $# (or some other way if u can suggest) how do I do it?? $"$#" `$"$#"` These don't work :( (4 Replies)
Discussion started by: amit_oddey21
4 Replies

10. UNIX for Dummies Questions & Answers

command line argument parsing

how to parse the command line argument to look for '@' sign and the following with '.'. In my shell script one of the argument passed is email address. I want to parse this email address to look for correct format. rmjoe123@hotmail.com has '@' sign and followed by a '.' to be more... (1 Reply)
Discussion started by: rmjoe
1 Replies
Login or Register to Ask a Question