1

I have a somewhat long path. I construct my path from these components.

PART1="/home/$USER/.local/bin:/home/$USER/bin:/usr/local/sbin:/usr/local/bin"
PART2="/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
PART3="/usr/local/bin:/home/local/bin:/usr/bin/mh:/home/$USER/go/bin"

export PATH=${PART1}:${PART2}:${PART3}

from within ~/.bash_profile running the above code renders what's below.

/home/kevinc/.local/bin:/home/kevinc/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/bin:/home/local/bin:/usr/bin/mh:/home/kevinc/.go/bin:/home/kevinc/go/bin

But, from a fresh terminal window that used ~/.bash_profile the path in that shell is below. Notice that the path "." got prepended, and that the last three directories didn't end up in the final result.

.:/home/kevinc/.local/bin:/home/kevinc/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/bin

I changed the order of PART2 & PART3 in my script, and the output directory list stayed in the above order.

I wondered if there is a maximum length for environment variable size. That questions has been answered numerous times by various people looking up limits source code used in Linux. Other decided to continue increasing their environment variable size until the program stopped working right. Those answers were in the 10k to 30M in size. I think that my 244 character size isn't likely a limit.

For what it's worth, I'm running Linux Mint 21.3, one rev. behind the newest version.

Yes, I ran my code through http://shellcheck.net

9
  • 5
    the leading . doesn't just show up on its own, it's being added somewhere; review all of your login/resource files (.bashrc, .profile, .bash_profile); do you recall if you've updated any of the default resource files (eg, /etc/bash.bashrc)? the fact you can change the order of appended variables but see no difference supports the fact that you're building PATH in some other file; as for the limitation on PATH length ... I'm running ubuntu 22.04 / bash 5.1.16 and I have no problem building PATH with your code (ie, I don't hit a length limitation) Commented Dec 3, 2024 at 23:05
  • 1
    Also see Is it safe to add . to my PATH? How come?. Commented Dec 3, 2024 at 23:23
  • 3
    Just to note, it's probably safer to only prepend your new directories (you almost never want or need to append to the existing path) to the existing value of $PATH, and there's little harm in simply writing a series of assignments like PATH=path1:$PATH; PATH=path2:$PATH; # etc`. Commented Dec 3, 2024 at 23:33
  • 1
    Two minor nits: (1) PART3 contains /home/$USER/go/bin but output ("from within ~/.bash_profile") has /home/kevinc/.go/bin:/home/kevinc/go/bin (note the additional .go/bin) so perhaps a copy-paste error along the way when posting; (2) PART1 and PART3 both contain /usr/local/bin so if length is a concern consider eliminating one (the dupe does show in final terminal window output as well). Commented Dec 4, 2024 at 0:08
  • 1
    what chepner said is the standard, if only de facto. it's very unusual to attempt to completely redefine your path. Your bash profile or bashrc or whatever can all separately use export PATH=/opt/path:$PATH as much as they want to populate the path variable without clobbering each other. Commented Dec 4, 2024 at 0:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.