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.

phuclv
  • 26,555
  • 15
  • 113
  • 235
user36302
  • 159
  • 4
  • 7
    `>` is not a command, it's a *redirection operator*. – Arkadiusz Drabczyk Mar 01 '19 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 01 '19 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 01 '19 at 16:12
  • 2
    Bash appears to offer a restricted mode: https://stackoverflow.com/questions/50989369/is-it-possible-to-disable-completely-linux-redirection-operators – music2myear Mar 01 '19 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 02 '19 at 11:34
  • https://support.rstudio.com/hc/en-us/articles/206382178-Customizing-Keyboard-Shortcuts – phuclv Mar 02 '19 at 15:24

1 Answers1

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

phuclv
  • 26,555
  • 15
  • 113
  • 235
  • 1
    This still wont prevent the creation of new files – D. Ben Knoble Mar 01 '19 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 07 '19 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 07 '19 at 16:17