A clearer way would be to establish a default for the user, then locate the Bash history file for that user.
You're making the assumption that the user's home directory will be in /home
, and that a root user's home directory will be /root
. That's a convention for Linux, but it won't be true at larger Linux installations, and it isn't the convention for other operating systems. Here's one way to do the lookup for systems that use GNU libc:
user="${1-root}"
histfile="$(getent passwd "$user" | cut -d: -f6)/.bash_history"
A cross-platform way is to use tilde expansion, though the eval
could be dangerous if the input is untrustworthy:
histfile=$(eval echo "~${1-root}")/.bash_history
The isRoot
check should be done on the username (or UID) instead of the home directory. It feels very unconventional to assign a boolean variable in Bash. Keep in mind that the convention is that zero statuses are considered "true" and non-zero statuses are considered "false". I'd advise you not to write that statement at all, but can't really advise you what to do instead since you haven't shown us what you intend to do with that variable.