LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-2021, 10:52 AM   #1
aikempshall
Member
 
Registered: Nov 2003
Location: Bristol, Britain
Distribution: Slackware
Posts: 853

Rep: Reputation: 122Reputation: 122
bash history


When I enter a multi-line command at the command line in bash like

Code:
echo "alex" | while read LINE
do
EDIT_STRING=$(echo alex | grep "${LINE}" )
if [[ ! -z "$EDIT_STRING" ]];then 
        echo $EDIT_STRING
        mktemp aik.XXXXXX
fi 
done
it ends up in the history file as

Quote:
echo "alex" | while read LINE; do EDIT_STRING=$(echo alex | grep "${LINE}" ); if [[ ! -z "$EDIT_STRING" ]];then echo $EDIT_STRING; mktemp aik.XXXXXX; fi ; done
It is retrieved from history as


Quote:
echo "alex" | while read LINE; do EDIT_STRING=$(echo alex | grep "${LINE}" ); if [[ ! -z "$EDIT_STRING" ]];then echo $EDIT_STRING; mktemp aik.XXXXXX; fi ; done

Is there a way in bash to replicate the ksh behavior whereby the command would be stored in history exactly as entered i.e. -

Quote:
805 echo "alex" | while read LINE
do
EDIT_STRING=$(echo alex | grep "${LINE}" )
if [[ ! -z "$EDIT_STRING" ]];then
echo $EDIT_STRING
mktemp aik.XXXXXX
fi
done
So that when it's retrieved from history it appears on the command line as exactly as entered initially.

Thanks
Alex

Last edited by aikempshall; 11-05-2021 at 10:54 AM. Reason: ficed gramatical error
 
Old 11-05-2021, 11:08 AM   #2
aikempshall
Member
 
Registered: Nov 2003
Location: Bristol, Britain
Distribution: Slackware
Posts: 853

Original Poster
Rep: Reputation: 122Reputation: 122
I think I've found my answer

Code:
shopt -s cmdhist lithist
Seems to do what I want.
 
1 members found this post helpful.
Old 11-05-2021, 11:52 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,782

Rep: Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456

As per the Bash shopt documentation, you only need the latter. The first is already on, otherwise your multi-line command wouldn't be saved as a single entry:
Quote:
Originally Posted by https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
cmdhist

If set, Bash attempts to save all lines of a multiple-line command in the same history entry. This allows easy re-editing of multi-line commands. This option is enabled by default, but only has an effect if command history is enabled (see Bash History Facilities).

lithist

If enabled, and the cmdhist option is enabled, multi-line commands are saved to the history with embedded newlines rather than using semicolon separators where possible.
 
Old 11-05-2021, 12:25 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,188

Rep: Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752
FISH does this OOTB.
 
Old 11-05-2021, 01:28 PM   #5
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDo, tinycore, Q4OS,Manjaro
Posts: 4,088

Rep: Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904
I often enter my compound commands on a single line. I do not often SCRIPT that way, but it makes for efficient use of command line and history space. If my history maintained expansion, I would have to set it back to the default behavior.

What I love about Linux, is that you can set things for your preference if ANYONE else in the development community has ever agreed with your usage! ;-)
 
Old 11-05-2021, 03:09 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,434
Blog Entries: 4

Rep: Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376
Dunno ... here's my advice:

(1) Write a short script. In a separate file that you mark "executable."

(2) Remember that you can "write a short script" in any language that you like, thanks to the "#!shebang" trick.

For instance, if you put something like #!/usr/bin/perl as the first line, then the remainder of the script can be written in Perl. And, no one will ever be the wiser, other than you. (Pick your favorite interpreted language of choice ...)

You've never been limited to "BASH scripting," and in my opinion it's usually not the best way to approach things. The only Shell that I'm aware of which ever tried to incorporate a "real" programming language was the Korn shell (ksh), and I've only encountered that one a couple of times "in the wild" in many decades.
 
Old 11-06-2021, 12:05 PM   #7
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDo, tinycore, Q4OS,Manjaro
Posts: 4,088

Rep: Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904Reputation: 1904
Quote:
Originally Posted by sundialsvcs View Post
Dunno ... here's my advice:

(1) Write a short script. In a separate file that you mark "executable."

(2) Remember that you can "write a short script" in any language that you like, thanks to the "#!shebang" trick.

For instance, if you put something like #!/usr/bin/perl as the first line, then the remainder of the script can be written in Perl. And, no one will ever be the wiser, other than you. (Pick your favorite interpreted language of choice ...)

You've never been limited to "BASH scripting," and in my opinion it's usually not the best way to approach things. The only Shell that I'm aware of which ever tried to incorporate a "real" programming language was the Korn shell (ksh), and I've only encountered that one a couple of times "in the wild" in many decades.
What does that have to do with shell (specifically bash) history? Just curious.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does BASH history command have no history when run in script ? lump_of_lard Programming 4 01-30-2015 10:45 AM
history clearance for a particular history number in linux sangupari Linux - Software 3 03-03-2010 04:08 AM
tcsh: can you save the history from multiple shells to one history file? BrianK General 2 04-23-2009 06:19 AM
Download history and copy history? inverted.gravity Linux - Newbie 1 02-21-2006 01:31 PM
How to find back "history" database after "history -c" ? san_lss Linux - Newbie 1 01-07-2004 12:53 PM

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

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