Can I Make Bash Work Like Zsh?
Asked by
CuriousCat92
I'm interested in sticking with Bash, but I'm wondering if there are ways to enhance its functionality to mimic some Zsh features. First, can I set up Bash to constantly write and read command history in real-time, like Zsh does? Additionally, can I run Bash scripts smoothly if I'm using Zsh in my terminal emulator, similar to how you can run .bat files from PowerShell in Windows?
Answers
To have Bash write and read command history in real-time, you can set this up: add `PROMPT_COMMAND+=( "history -a; history -n" )` to your configuration. This way, every time a prompt appears, Bash writes new history entries to the file and loads any new ones from other sessions. Just a heads up, you might need to hit enter to trigger the history read in your current session, unlike Zsh which does this automatically. But if you're getting good results from it, that's awesome!
Reply to this answer
Honestly, if youโre contemplating these features, why not just switch to Zsh? Most people find it a worthwhile change.
Reply to this answer
Just a fun fact: Zsh actually has a Bash emulation mode. But regardless of the shell you use, the shell scripts themselves can remain the same. I always start my scripts with `#!/bin/sh` for compatibility, but switching to a Bash shebang is totally fine too if thatโs what you prefer!
ScriptingNoob23: Thatโs the point; if I have to use Zsh, I still want my Bash scripts to run by default.
Reply to this answer
If you're looking for some cool improvements, check out `ble.sh`. It offers syntax highlighting, enhanced autocompletion, and a Vim editing mode. Itโs pretty robust, and after some tweaks, I've found it integrates nicely with the atuin project for better history management.
ScriptSavvy21: Sounds interesting, Iโll definitely give it a try!
Reply to this answer
Yes, you can run Bash scripts from Zsh simply by calling `bash` directly, or if your script includes a shebang and is executable, you can run it directly just like you would any binary. For example, you can write a script like this in `tmp.sh`:
```bash
#!/bin/bash
echo "Bash version: $BASH_VERSION"
```
Make sure to give it execution permissions, and you can run it without issues!
ScriptingNoob23: I always use the shebang at the start of my scripts too; itโs just a good habit!
Reply to this answer
Add your answer
Answers are reviewed before publication.
LinuxNinja42: Nice! Didnโt know about using PROMPT_COMMAND as an array. This definitely helps!
BashBuster18: I had to enter an empty command too for the history to update. Wonder if Zsh is really as real-time as they say. Regardless, glad it's working!