LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2012, 01:42 AM   #1
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Rep: Reputation: 0
Command line history scripts


Hi
I frequently enter commands on the Bash command line which extend over a couple of lines eg.
Code:
for i in files-*.log
if [ -e ${i/log/tmp} ]
then
cp $i ${i/log/out}
fi
done
Manytimes, I build up such a script, where I initially just write an `echo' command instead of `cp' to prevent misfortune. However, each time I use key-up to get the previous entry from the command line, the whole command is written on one line and it becomes difficult to edit. Can Bash be configured such that multi line commands from the history are "rebuilt" that way?

Thanks for hints.
 
Old 11-05-2012, 05:06 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,837

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
in vi mode you can simply press key up to look for the command and ESC and v to edit that command
in emacs mode there can be something similar, but I'm not really familiar with that mode
 
Old 11-05-2012, 05:43 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by mzh View Post
Can Bash be configured such that multi line commands from the history are "rebuilt" that way?
Not sure if that could be done in bash, and sometimes the commands are built the way they should, but sometimes just creating and using a temporary script for it, is not really a bad thing and even safer. That's how I do things when I find that I'm already creating a large multi-line command and that I'm no longer really able to handle it.
 
Old 11-05-2012, 07:10 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-06-2012, 01:40 AM   #5
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
in vi mode you can simply press key up to look for the command and ESC and v to edit that command
in emacs mode there can be something similar, but I'm not really familiar with that mode
Hey, thanks for pointing this out. I checked out the description here
http://www.catonmat.net/blog/bash-vi...e-cheat-sheet/

where vi mode of bash is illustrated and this guy gives an example where he enters a command on the Bash command line, presses ESC to enter command mode (leaving insert mode) and 'v' (no quotes) to open a temporary vi session with the command currently being edited. Here it's fairly easy to edit the multi-line command, but what I dont get is how then to correctly execute the modified command?
A guy in the comments says you need :wq (no quotes), and that seems to execute the command, however my prompt gets stuck on a line with a '>' in front of it and i need to use Ctrl-C to finish it. I guess i'm missing how to correctly execute the temporary command from the vi session. How do I do that?

thank again
 
Old 11-06-2012, 02:21 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,837

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
in such cases the command is copied into a temp file and that temp file will be edited by vi. You need to save that file before exiting vi (that is :wq) and finally the content of that temp file will be executed as a command (and also the temp file will be removed). If you got prompt '>' it means the command was not complete, could not be executed (for example a done, esac, fi or similar is missing from the end).
 
Old 11-06-2012, 10:43 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The lithist shell option makes the shell save multi-line commands as-is.

Code:
shopt -s lithist
Put it in your .bashrc to make it "permanent".

Note that using it comes with its own set of headaches as well.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command line history crickett Linux - General 13 12-17-2009 08:22 AM
line command history mfran2002 Linux - Software 5 10-25-2006 08:33 AM
clean command line history basilogics Linux - Newbie 9 11-04-2005 04:25 AM
command line history begby Linux - Software 7 03-12-2005 01:58 AM
history with command line compu73rg33k Linux - Software 3 02-16-2004 03:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:22 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