LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-08-2009, 07:07 AM   #1
babu198649
Member
 
Registered: Oct 2007
Posts: 160

Rep: Reputation: 30
history -c in shell file does not work


Hi
I want to write a script which deletes the shell history.(ie) i want to execute the command "history -c" through a shell file.I have put the command history -c in the shell file and made the shell file to have execute permissions(chmod +x).But this is not working ,Can anyone point the error.

Thanks in advance.
 
Old 05-08-2009, 07:52 AM   #2
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
This is because it is not executed in the current shell. A shell script looks like this (Assume its name is script.sh)
Quote:
#!/bin/sh

cmd1
cmd2
history -c
You usually run it as ./script.sh

The first line causes a new shell to be spawn. The commands in the file are executed in that shell. Hence it does not clear the history from your current shell. To circumvent this, run the script as shown below

source ./script.sh
 
Old 05-09-2009, 01:13 AM   #3
babu198649
Member
 
Registered: Oct 2007
Posts: 160

Original Poster
Rep: Reputation: 30
Thanks it works,
Is there a way to execute the script like a normal script, without using the source program.
 
Old 05-09-2009, 02:34 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by babu198649 View Post
Thanks it works,
Is there a way to execute the script like a normal script, without using the source program.
Nope. If you want to clear the history of the parent shell you have to source it. No other way allowed.
 
Old 05-09-2009, 08:23 AM   #5
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Quote:
Originally Posted by babu198649 View Post
Thanks it works,
Is there a way to execute the script like a normal script, without using the source program.
Most linux systems will have a hidden file called .bash_logout in the home directory. If you don't have one, just create it and add whatever commands you wish to perform after logging out.

This is how I clear my shell history and when you log back in, you'll have a fresh history to start with.

Hope this helps.

Last edited by dv502; 05-09-2009 at 08:31 AM.
 
Old 05-09-2009, 08:32 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Use an alias or a function instead of writing a script for such simple task. That way it will be ran inside the context of the current shell.
 
  


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
tcsh: can you save the history from multiple shells to one history file? BrianK General 2 04-23-2009 05:19 AM
Read command in bash shell file doesn't work jh111451 Linux - Software 4 07-28-2008 08:35 PM
BASH shell temporary history WingnutOne Linux - Newbie 10 09-16-2007 01:37 AM
Shell History paraiso Linux - General 3 07-27-2006 04:17 AM
Erase shell history mikeshn Linux - General 1 04-25-2004 08:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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