1

I created a shell script file named 1.sh with only one echo command:

echo $HISTSIZE

I made the file 1.sh executable and tried to run the file in the terminal by running the command

./1.sh

It printed an empty line on the terminal. But when I run this command at the shell prompt I get 1000 as output.

Why is the echo not working in my shell script?

3

You script is running in a non-interactive shell, which has history disabled by default (assuming it supports history, but that’s the case in most sh implementations nowadays); thus HISTSIZE is empty.

If you forcibly enable history (in Bash at least), you’ll get the default value:

set -o history
echo $HISTSIZE
  • But i have a query. When i just echo $HISTSIZE in commandline ,i get a value 1000. But when i does it by enabling history in shell script, i get a value 500. Why there is a difference in two values? – FortuneCookie Dec 17 '17 at 10:05
  • You get different values because, when the shell runs a script, it doesn’t process your startup scripts (.profile, .bashrc etc.). Thus HISTSIZE is set to the default value, 500, rather than the value your startup scripts set, 1000. – Stephen Kitt Dec 17 '17 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.