-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bash: Fix bashisms being applied to non-bash shells #4334
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,23 +1,53 @@ | ||||||||||||
# Begin /usr/share/defaults/etc/profile.d/50-prompt.sh | ||||||||||||
|
||||||||||||
# We cant use $SHELL since we need the *current* shell. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the prompt is only really relevant for interactive shells, maybe return early if the terminal is not interactive?
Suggested change
|
||||||||||||
# The user may switch to a different shell than their default | ||||||||||||
# e.g. $SHELL is zsh but user switches to bash temporarily | ||||||||||||
|
||||||||||||
current_shell=$(readlink /proc/$$/exe) | ||||||||||||
|
||||||||||||
endchar="\$" | ||||||||||||
if [ "$UID" = "0" ]; then | ||||||||||||
endchar="#" | ||||||||||||
elif [[ "$current_shell" =~ zsh ]]; then | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure this logic is sound. What I would do is to add a check like this:
... as those change in a shell-defined way, depending on whether a shell is running as euid 0 or not. |
||||||||||||
endchar='%' | ||||||||||||
fi | ||||||||||||
|
||||||||||||
FG="\[\033[38;5;081m\]" | ||||||||||||
BG="\[\033[38;5;245m\]" | ||||||||||||
AT="\[\033[38;5;245m\]" | ||||||||||||
HCOLOR="\[\033[38;5;206m\]" | ||||||||||||
# Prefer tput to ANSI codes since they work with bash and zsh | ||||||||||||
# and are much easier to read | ||||||||||||
grey=$(tput setaf 245) | ||||||||||||
pink=$(tput setaf 206) | ||||||||||||
cyan=$(tput setaf 81) | ||||||||||||
reset_color=$(tput sgr0) | ||||||||||||
|
||||||||||||
PS1="${FG}\u${AT}@${HCOLOR}\H ${BG}\w ${FG}$endchar \[\e[0m\]" | ||||||||||||
FG=$cyan | ||||||||||||
DIR=$grey | ||||||||||||
AT=$grey | ||||||||||||
HCOLOR=$pink | ||||||||||||
|
||||||||||||
# bash and zsh handle prompt setup and character escaping differently | ||||||||||||
|
||||||||||||
if [[ "$current_shell" =~ bash ]]; then | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is another instance where the |
||||||||||||
PS1="\[${FG}\]\u\[${AT}\]@\[${HCOLOR}\]\H \[${DIR}\]\w \[${FG}\]$endchar \[${reset_color}\]" | ||||||||||||
elif [[ "$current_shell" =~ zsh ]]; then | ||||||||||||
PS1="%{${FG}%}%n%{${AT}%}@%{${HCOLOR}%}%m %{${DIR}%}%1~ %{${FG}%}$endchar# ${reset_color}" | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taken together with my suggestion above, the Note also that this uses double brackets, which is not very POSIX-y. |
||||||||||||
fi | ||||||||||||
|
||||||||||||
unset FG | ||||||||||||
unset BG | ||||||||||||
unset AT | ||||||||||||
unset HCOLOR | ||||||||||||
if [ $SHELL != "/bin/zsh" ]; then | ||||||||||||
unset grey | ||||||||||||
unset pink | ||||||||||||
unset cyan | ||||||||||||
unset reset_color | ||||||||||||
unset endchar | ||||||||||||
|
||||||||||||
# shopt is bash only | ||||||||||||
if [[ "$current_shell" =~ bash ]]; then | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more instance where |
||||||||||||
shopt -s checkwinsize | ||||||||||||
fi | ||||||||||||
|
||||||||||||
unset current_shell | ||||||||||||
|
||||||||||||
# End /usr/share/defaults/etc/profile.d/50-prompt.sh |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Files in this directory must have code that is POSIX compliant, since the file is loaded by all BOURNE compatible shells | ||
If you must use a bash-only function like shopt it must be put in a block that will load it only for bash | ||
|
||
e.g. | ||
|
||
``` | ||
current_shell=$(readlink /proc/$$/exe) | ||
if [ $current_shell = "/usr/bin/bash" ]; then | ||
shopt -s checkwinsize | ||
fi | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
source /usr/share/defaults/etc/profile | ||
|
||
# User specific bash aliases and functions | ||
if [ -d ~/.bashrc.d ]; then | ||
for rc in ~/.bashrc.d/*; do | ||
if [ -f "$rc" ]; then | ||
. "$rc" | ||
fi | ||
done | ||
unset rc | ||
fi |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ | |
<Name>bash</Name> | ||
<Homepage>https://www.gnu.org/software/bash</Homepage> | ||
<Packager> | ||
<Name>Reilly Brogan</Name> | ||
<Email>[email protected]</Email> | ||
<Name>Tracey Clark</Name> | ||
<Email>[email protected]</Email> | ||
</Packager> | ||
<License>GPL-3.0-or-later</License> | ||
<PartOf>system.base</PartOf> | ||
|
@@ -135,7 +135,7 @@ | |
</Description> | ||
<PartOf>programming.devel</PartOf> | ||
<RuntimeDependencies> | ||
<Dependency release="84">bash</Dependency> | ||
<Dependency release="85">bash</Dependency> | ||
</RuntimeDependencies> | ||
<Files> | ||
<Path fileType="header">/usr/include/bash/alias.h</Path> | ||
|
@@ -201,12 +201,12 @@ | |
</Files> | ||
</Package> | ||
<History> | ||
<Update release="84"> | ||
<Date>2024-10-18</Date> | ||
<Update release="85"> | ||
<Date>2025-01-03</Date> | ||
<Version>5.2.37</Version> | ||
<Comment>Packaging update</Comment> | ||
<Name>Reilly Brogan</Name> | ||
<Email>[email protected]</Email> | ||
<Name>Tracey Clark</Name> | ||
<Email>[email protected]</Email> | ||
</Update> | ||
</History> | ||
</PISI> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
source /usr/share/defaults/etc/zsh/zprofile | ||
|
||
# User specific zsh scripts and functions | ||
if [ -d ~/.zshrc.d ]; then | ||
for rc in ~/.zshrc.d/*; do | ||
if [ -f "$rc" ]; then | ||
. "$rc" | ||
fi | ||
done | ||
unset rc | ||
fi |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,17 @@ | ||
## DO NOT edit this file. Your changes will be overridden on package updates. | ||
# Instead add your changes to a new profile script under /etc/profile.d/ (create this directory if it does not exist) for system wide use or | ||
# ~/.zshrc.d for an individual user | ||
|
||
# Begin /usr/share/defaults/etc/zprofile | ||
|
||
if [ -f /etc/zprofile ]; then | ||
source /etc/zprofile | ||
elif [ -f /etc/zsh/zprofile ]; then | ||
source /etc/zsh/zprofile | ||
fi | ||
|
||
# Source paths that may not be included by systemctl | ||
if [ -f /usr/share/defaults/etc/profile.d/10-path.sh ]; then | ||
emulate sh -c 'source /usr/share/defaults/etc/profile.d/10-path.sh' | ||
fi | ||
|
||
# Source paths not included by systemctl necessary for XDG_DATA_DIRS | ||
|
||
if [ -f /usr/share/defaults/etc/profile.d/10-xdg.sh ]; then | ||
emulate sh -c 'source /usr/share/defaults/etc/profile.d/10-xdg.sh' | ||
fi | ||
|
||
# Source paths not included by systemctl necessary for flatpaks to be found in the app menu and run | ||
|
||
if [ -f /usr/share/defaults/etc/profile.d/70-flatpak.sh ]; then | ||
emulate sh -c 'source /usr/share/defaults/etc/profile.d/70-flatpak.sh' | ||
fi | ||
|
||
# Source paths not included by systemctl necessary for snaps to be found in the app menu and run | ||
|
||
if [ -f /usr/share/defaults/etc/profile.d/70-snapd.sh ]; then | ||
emulate sh -c 'source /usr/share/defaults/etc/profile.d/70-snapd.sh' | ||
fi | ||
|
||
# Source gtk modules variable for menus | ||
|
||
if [ -f /usr/share/defaults/etc/profile.d/appmenu-gtk2-module.sh ]; then | ||
emulate sh -c 'source /usr/share/defaults/etc/profile.d/appmenu-gtk2-module.sh' | ||
fi | ||
|
||
# Paths for gawk | ||
|
||
if [ -f /usr/share/defaults/etc/profile.d/gawk.sh ]; then | ||
emulate sh -c 'source /usr/share/defaults/etc/profile.d/gawk.sh' | ||
fi | ||
|
||
# Environment for vte, zsh compatible | ||
|
||
if [ -f /usr/share/defaults/etc/profile.d/vte.sh ]; then | ||
source /usr/share/defaults/etc/profile.d/vte.sh | ||
fi | ||
# This should always be sourced regardless of the above files | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block should probably be at the top of the script, so the system zprofile in /etc can override vendor config settings...? |
||
# Warning: Do not remove the line below, otherwise it will break the integrity of other packages which provide some scripts in /etc/profile.d/. | ||
emulate sh -c 'source /usr/share/defaults/etc/profile' | ||
|
||
# Do not source /etc/profile as Arch does | ||
# This will source all other bash scripts in /etc/profile.d, some of which may not load properly. | ||
# Some may also conflict with zsh frameworks or have other unintended consequences | ||
# Additionally, Solus sources ~/.bashrc.d from /etc/profile which is not obvious to the user or expected in zsh | ||
# End /usr/share/defaults/etc/zprofile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out that
... is a very effective way to check if the running shell is bash.