5

For an interactive session via SSH on a Linux cluster in Rstudio, it is very easy to accidentally execute the keyboard shortcut for "run in terminal" when commands are highlighted in Rstudio console. This results in behavior such as

\> library

or

Var1>Var2

being executed in terminal, which creates a library or Var2 file in the remote working directory if the terminal is logged in at the bash prompt.

In a worst case scenario this would result in overwriting files in the remote working directory.

I would like to disable ">" in these sessions, but I am not sure how to do this.

6
  • 7
    > is not a command, it's a redirection operator. Mar 1, 2019 at 16:07
  • 3
    Redirection characters are fundamental to Linux shells and can't be changed. You should look at whether you can change the Rstudio prompt to something less drastic if accidentally copied. If you can't, you'll just need to take more care.
    – AFH
    Mar 1, 2019 at 16:12
  • ok that explains why it can't be aliased, but do you have a suggestion how to address my question?
    – user36302
    Mar 1, 2019 at 16:12
  • 2
    Bash appears to offer a restricted mode: stackoverflow.com/questions/50989369/… Mar 1, 2019 at 16:13
  • 2
    Isn't this an XY problem? Why don't you disable the "run in terminal" shortcut, or at least make it something else harder to stroke instead? Even with the noclobber solution, your code may still contain >| inside it... although it's unlikely, depends on the programming language.
    – user202729
    Mar 2, 2019 at 11:34

1 Answer 1

23

> is not a command but an operator for redirecting streams, so you can't alias it. But you can disable overwriting files with the noclobber option. Just run set -C or set -o noclobber (or add them to your ~/.bashrc)

The noclobber option is available to avoid overwriting existing files with output redirection (see The Set Builtin). The ‘>|’ redirection operator may be used to override noclobber.

If the file didn't exist it'll still be created, but at least you can avoid the worst case scenario. But you may still have problem if there are >| in your code

If you want to completely disable the redirection, you have to use the restricted shell but that'd result in an extremely limited environment, most notably you can't run commands with slashes like /bin/ls or change the directory

The best solution is to change the "run in terminal" shortcut in RStudio which was explain clearly in their website

3
  • 1
    This still wont prevent the creation of new files Mar 1, 2019 at 23:40
  • This is a great answer as far as it goes. I set noclobber, but I would still like to avoid generating new files. Are you able to address the "?" issue I raised in the edited question? Thanks!
    – user36302
    Mar 7, 2019 at 15:05
  • I don't know R and have never used Rstudio, so I don't know about that. But as commented, this might be an XY problem, and you're better off changing the Rstudio or terminal shortcut. But if you really want to force a restricted shell then just add rbash to the end of ~/.bashrc or ~/.profile
    – phuclv
    Mar 7, 2019 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .