0

So I'm trying to teach myself Unix and I changed my MacOS terminal shell from the default zsh to bash, because the Coursera courses that I studied use Bash scripting and so does the First Semester in you CS Education that I intend to follow along.

I downloaded the latest bash using brew install and change the default to bash using chsh -s command.

I noticed, when I'm trying to learn bash history today, that my bash doesn't seem to store history command to the .bashhistory file.

For one thing, I'm unable to open the bashhistory file, supposedly because of the security setup of MacOS, but I just noticed that I lost all the history once I closed my terminal.

This is the code I run

apples-MacBook-Pro:~ apple$ sudo cat ~/.bash_history
sudo zsh
apples-MacBook-Pro:~ apple$ 

It goes without saying that I have more history command than just that. Also, I can't use up arrow to retreive historical command, at least not the ones that I typed in before closing off the old terminal and opened a new one, even though I distinctly remember being able to do that for the zsh shell.

BTW, I believe the history size is set to 500, rather than 1, which was my original suspision.

apples-MacBook-Pro:~ apple$ echo $HISTSIZE
500

Thanks in advance for your time.

4
  • Your sudo here is a bit suspicious. Can you as a normal user read this file? (If not, why not?) What owner and what permissions does the file have according to ls -l?
    – user1686
    Oct 29, 2021 at 13:42
  • Hi sir!Thank you so so much for taking the time. Without using sudo, I got permission denied error. I cannot read the file. Here is the result after running ls -l ~/.bash_history -rw------- 1 root staff 9 Apr 2 2021 /Users/Yichen/.bash_history
    – jxhyc
    Oct 29, 2021 at 13:49
  • I'm thinking that maybe it is because I have two bash installed. The old bash that come with Mac, and the new one I installed using brew. I am sure I'm running the new one with bash--version command showing 5.18, but maybe ~/.bash_history is not the right place to look for my bash history?
    – jxhyc
    Oct 29, 2021 at 13:52
  • This would appear to be a continuation of superuser.com/questions/1684495/…
    – Tetsujin
    Oct 29, 2021 at 13:54

1 Answer 1

0

Your ls -l output shows that, just like last time, the file is owned by root and doesn't have permissions granted to anyone else:

-rw------- 1 root staff 9 Apr 2 2021 /Users/Yichen/.bash_history

This is most likely because you used sudo -E bash or something similar in the past, which ran bash under root's UID but still with your original $HOME, so it ended up writing root's history to your home directory. (If you didn't use the -E option, then it could be that macOS' sudo is configured to preserve $HOME by default, in which case the -H option would avoid the problem.)

Either remove the file and let bash re-create it properly, or use sudo chown to change its owner back to yourself.

Use find ~ -user root -exec ls -ld {} \; to find other files which might be similarly affected.

1
  • Thank you so so much. You have already helped me a lot in one day! I promise when I become as knowledgeale as you are, I will repay the generosity back to the community:)
    – jxhyc
    Oct 29, 2021 at 14:48

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.