-1

I have a screen session that is always running in a separate user (for a minecraft server). Each time I want to attach to this screen, I use su -c 'screen -r mc-1.18.2' minecraft to attach back to this screen.

I wanted to make an alias for this command just so I didn't have to type it out or go up in the command history each time. I added alias mcconsole="su -c 'screen -r mc-1.18.2' minecraft" to my ~/.bash_aliases folder. My other aliases work just fine but when I run mcconsole I am left with a brand new screen session. I am not sure where the hiccup is when attaching to the screen session (which is running as I can attach to it normally).

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
That_Dude
  • 11
  • 3
  • The alias should work just like typing the command by itself. I can't think of a reason why it would start a new session. – Barmar Jun 09 '22 at 21:18

2 Answers2

1

maybe try to invert simple and double quotes, something like:

alias mcconsole='su -c "screen -r mc-1.18.2" minecraft'

or escape double quotes inside:

alias mcconsole="su -c \"screen -r mc-1.18.2\" minecraft"

busshi
  • 66
  • 2
0

Sounds like something screwy is happening with quoting when using the alias. Try using a shell function instead.

mcconsole() {
    su -c 'screen -r mc-1.18.2' minecraft
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for the quick response! When making a function, if I want it to be accessible in any scope like an alias, do I place it in `~/.bashrc` or a different file? – That_Dude Jun 09 '22 at 21:29
  • You can put it in `.bash_aliases` and it will be defined just like your aliases. – Barmar Jun 09 '22 at 21:31
  • This solution also works but I had some trouble getting it to work with `sudo`. Thanks for the help! – That_Dude Jun 09 '22 at 21:49
  • I don't see why that would be any different. – Barmar Jun 09 '22 at 21:52
  • This might just be from being a bit uninformed, but I get a "command not found" when I use sudo to this function. I fixed this for the alias solution by adding `alias sudo='sudo '` to my `~/.bashrc`. – That_Dude Jun 09 '22 at 22:00
  • You can't run aliases or functions with `sudo`, only external commands. – Barmar Jun 09 '22 at 22:01
  • Put sudo in the function body, rather than trying to run the function with sudo. – Barmar Jun 09 '22 at 22:02
  • `sudo -u minecraft screen -r mc-1.18.2` – Barmar Jun 09 '22 at 22:02