The difference between `$PATH` and `echo $PATH`

TL;DR: $PATH will call the value stored in the environmental variable PATH as a command, but echo $PATH will print out the value stored in the variable PATH.

How I found out:

Recently I have been trying to integrate WSL with RStudio.

One thing intrigues me is that the bash in Rmd has a different PATH with WSL. For example, one cannot run the software installed with conda directly from Rmd - the conda bin directory needs to be added into PATH (using export ...) for the subseuqent lines that call conda software to work. This prompts me to investigate how different they are.

However, I made a mistake when I tried to see what are the directories under PATH: I used $PATH instead of echo $PATH. This runs into an error

$PATH

output error:

/mnt/c/Users/PGDC-2/AppData/Local/Temp/RtmpMLmyMk/chunk-code-9cac2b41d4.txt: line 1: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/R/R-3.5.3/bin/x64:/mnt/c/Rtools/bin:/mnt/c/Program: No such file or directory

At first I thought that the error could be because the space between Program and File prevents bash from properly reading “/mnt/c/Program File” as PATH. However, when I ran the same command on a Linux desktop with no PATH directory name containing a space, the same error occurs. Thus, I began to suspect that bash may actually run the variable stored in the function as a command. Thus, I did the following to confirm:

pwd
CD=cd
cd /
pwd
$CD
pwd

output:

/mnt/c/Users/PGDC-2/OneDrive/liutiming/content/post
/
/home/pgdc

This proves that cd was run in the third line when $CD was run on that line. By revealing the function of directly calling the environemntal variable, this experiment also differentiates the two commands from each other.

NOTE: I tried to create the page in rmd with bash code chunk but there were errors when the page was rendered. Thus, I used markdown’s code chunk instead. Will investigate this issue on another day with a linux machine to localise the issue.

Avatar
Tim

Personalizing medicine