Skip to content

Commit f812dcd

Browse files
feat: create bin dir
This creates a bin/ directory in the user's home directory and puts it in the path
1 parent 22fef2c commit f812dcd

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

scripts/add-bin-dir.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Define the bin directory path
4+
BIN_DIR="$HOME/bin"
5+
6+
# Create the bin directory if it doesn't exist
7+
if [ ! -d "$BIN_DIR" ]; then
8+
mkdir -p "$BIN_DIR"
9+
echo "Created $BIN_DIR"
10+
else
11+
echo "$BIN_DIR already exists."
12+
fi
13+
14+
# add bin to the PATH only if it's not already there
15+
update_path() {
16+
local profile=$1
17+
if ! grep -q "PATH=.*:$BIN_DIR" "$profile" && ! grep -q "PATH=$BIN_DIR:.*" "$profile"; then
18+
echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$profile"
19+
echo "Added $BIN_DIR to PATH in $profile"
20+
else
21+
echo "$BIN_DIR is already in the PATH in $profile"
22+
fi
23+
}
24+
25+
# Check for zsh or bash and their respective files
26+
if [ -n "$ZSH_VERSION" ]; then
27+
if [ -f "$HOME/.zshrc" ]; then
28+
update_path "$HOME/.zshrc"
29+
fi
30+
elif [ -n "$BASH_VERSION" ]; then
31+
if [ -f "$HOME/.bash_profile" ]; then
32+
update_path "$HOME/.bash_profile"
33+
elif [ -f "$HOME/.bashrc" ]; then
34+
update_path "$HOME/.bashrc"
35+
elif [ -f "$HOME/.profile" ]; then
36+
update_path "$HOME/.profile"
37+
fi
38+
else
39+
echo "Unrecognized shell or profile file not found."
40+
fi
41+
42+
# Apply the changes by sourcing the profile files
43+
if [ -n "$ZSH_VERSION" ] && [ -f "$HOME/.zshrc" ]; then
44+
source "$HOME/.zshrc"
45+
elif [ -n "$BASH_VERSION" ]; then
46+
if [ -f "$HOME/.bash_profile" ]; then
47+
source "$HOME/.bash_profile"
48+
elif [ -f "$HOME/.bashrc" ]; then
49+
source "$HOME/.bashrc"
50+
elif [ -f "$HOME/.profile" ]; then
51+
source "$HOME/.profile"
52+
fi
53+
fi

0 commit comments

Comments
 (0)