LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   history -c in shell file does not work (https://www.linuxquestions.org/questions/linux-newbie-8/history-c-in-shell-file-does-not-work-724653/)

babu198649 05-08-2009 07:07 AM

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.

sarin 05-08-2009 07:52 AM

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

babu198649 05-09-2009 01:13 AM

Thanks it works,
Is there a way to execute the script like a normal script, without using the source program.

colucix 05-09-2009 02:34 AM

Quote:

Originally Posted by babu198649 (Post 3535223)
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.

dv502 05-09-2009 08:23 AM

Quote:

Originally Posted by babu198649 (Post 3535223)
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.

i92guboj 05-09-2009 08:32 AM

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.


All times are GMT -5. The time now is 08:23 AM.