DJ. Joe
DJ. Joe

Reputation: 1

Wrong BASH-Variable return from a bash script

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.

Upvotes: 0

Views: 89

Answers (1)

chepner
chepner

Reputation: 531275

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.

Upvotes: 1

Related Questions