Ubuntu – Terminal not taking the full input of a directory

bashcommand linegnomegnome-terminalsublime-text

I am trying to understand the functioning of the Build Systems of Sublime Text 3. The commands going into the shell_cmd are the ones which will go directly into the terminal, but the terminal shows "No such file or directory exists". Here's the build system I made.

{
  "shell_cmd": "gnome-terminal -- bash -c '${file_path}/${file_base_name}; read -sn 1'",
  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
  "working_dir": "${file_path}",
  "selector": "source.c, source.c++, source.cxx, source.cpp",
  "variants":
  [
      {
          "name": "Run",
          "shell": true,
          "shell_cmd": "gnome-terminal -- bash -c '${file_path}/${file_base_name}; read -sn 1'"
      }
  ]
}

enter image description here

The actual file path is :
'/home/dr_insult/COMPUTER PROGRAMS/DATA STRUCTURES & ALGORITHMS/Stacks & Queues/'. I know that it is caused because there is a space between words, but shouldn't that have been corrected by the '' quotes in the build system?

Best Answer

Your issue is that you need to encapsulate your path with quotes. It may look that you already are, but I guess you are getting confused by the number of "string in a string" you have.

Starting with your JSON, you are passing the value of the shell_cmd key to your program which is:

gnome-terminal -- bash -c '${file_path}/${file_base_name}; read -sn 1'

That command is starting the gnome-terminal application and asking it to run the following command on startup:

bash -c '${file_path}/${file_base_name}; read -sn 1'

That command, in turn, is executing bash and telling it to run the following command - Take note of the non-existing quotes:

${file_path}/${file_base_name}; read -sn 1

Before running, that command will replace the variable with their appropriate value before being executed, and according to the error message you showed, the command is becoming the following:

/home/dr_insult/COMPUTER PROGRAMS/DATA STRUCTURES & ALGORITHMS/Stacks & Queues/; read -sn 1

Finally, what is happening, in this case, is that bash is parsing that command by splitting it on the & character as it thinks it should run several commands in the backgrounds like so:

# This first command, for example, is attempting to run a script named
# /home/dr_insult/COMPUTER while passing two parameters
# "PROGRAMS/DATA" and "STRUCTURES" and attempting to put it in the background
/home/dr_insult/COMPUTER PROGRAMS/DATA STRUCTURES &
ALGORITHMS/Stacks &
Queues/
read -sn 1

All of those paths don't exist in your system, which it raising those errors you see.

What you need to do is to quote your that command in order to tell bash to treat all that string as a single path.

So change the value of the shell_cmd key to the following (You will need to escape the double quotes so your JSON stays valid)

"gnome-terminal -- bash -c '\"${file_path}/${file_base_name}\"; read -sn 1'"
Related Question