0

I'd like to check the value of $HISTFILE (or any similar BASH-Variable) by a bash script. On the command console 'echo $HISTFILE' is the way I normally go, but from inside a bash script, which only includes:

    #!/bin/bash
    echo $HISTFILE

gives an empty line instead of showing $HOME/$USER/.bash_history (or similar return values). My questions are:

  1. What is the reason for doing so (since I never had such trouble using bash scripts) and
  2. how can I check the value of BASH-Variables like $HISTFILE from inside a bash script?

Many thanks in advance. Cheers, M.

1 Answer 1

1

HISTFILE is only set in interactive shells; scripts run in non-interactive shells. Compare

$ bash -c 'echo $HISTFILE' # non-interactive, no output

$ bash -ic 'echo $HISTFILE' # interactive, produces output
/home/me/.bash_history

However, forcing the script to run in an interactive shell will also cause your .bashrc file to be sourced, which may or may not be desirable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.