The context for this is that I want to read the user's path from xmonad, where the path is modified by whatever's in their .bashrc if applicable.

Here's what I find weird: if I have xmonad execute bash -cl "env && echo $PATH" it returns an env which includes one $PATH which does not match that returned by echo. (I want the former.)

What gives?

And is there a way to fix this, other than parsing the env output?


The shell is substituting the value of $PATH first, because of the double quotes. I'm not sure what the first set of double quotes is doing, but try one of

'bash -cl "env && echo $PATH"'
"bash -cl 'env && echo $PATH'"

Now, are you trying to extract just the path from the env output? If so, echo is not the tool. You'd want env | grep ^PATH=

  • I've edited the original post, my formatting was unclear: the outside quotes were just to set off the command being run. I'm trying to get the $PATH (as possibly modified by ~/.bash_profile and such), I'm just doing both env and echo to compare the outputs as a troubleshooting measure. env works, but I'm interested to know why echo echo doesn't. – Ben Kraft Aug 20 '11 at 16:45
  • The crux of this answer still stands. The echo $PATH part was evaluated by your current shell and its expansion passed to the subshell. Use single quotes or do this instead: echo "$(env && echo $PATH)" – bahamat Aug 20 '11 at 18:23
  • This was reasonably close to what worked; part of the issue was, I think, how I was spawning the process in xmonad. But it definitely helped, so thanks all! – Ben Kraft Aug 20 '11 at 19:20

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.