using sudo inserts '[' before command

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
willem_mint
Level 1
Level 1
Posts: 2
Joined: Fri Oct 21, 2022 6:00 am

using sudo inserts '[' before command

Post by willem_mint »

Okay, weird and annoying issue.
Whenever I use any command with sudo, an opening bracket character is inserted before the command.
This causes an 'unknown command' response, after which the command is executed anyway.
This causes havoc when chaining commands.

eg.

Code: Select all

$ sudo mkdir test
results in (translated into English by me)

Code: Select all

unknown command '[mkdir]', did you mean: 
 command '[mkdir]' in deb coreutils (8.30-3ubuntu2) 
 (... and so on)
then it just promps for password and after authorisation just goes ahead and executes the command.

Anyone have any idea what could be causing this?

Mint v 20.3 64b
Cinnamon v 5.2.7,
bash v 5.0.17(1) release
Gnome Terminal 3.36.2

I don't know what other info might be relevant to the issue.

I'd appreciate any clue as to what's happening here.
Last edited by LockBot on Fri Apr 21, 2023 10:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
karlchen
Level 23
Level 23
Posts: 18239
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: using sudo inserts '[' before command

Post by karlchen »

Hello, willem_mint.

Could you please share the complete terminal output of the commandline sudo mkdir test?
I.e. the commandline and everything which gets displayed? Not just some part.
And in this case, please, do not translate the output.
(If necessary I will manage to add the translation from Dutch to English. :wink: )

Could you also please share the output of this command: which sudo. - Reason for asking: I wonder a bit whether some script named "sudo" might be found following the $PATH variable, instead of the real "sudo" executable. Because "sudo" does not insert any characters into the commandline that follows "sudo".

Regards,
Karl
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 792 days now.
Lifeline
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: using sudo inserts '[' before command

Post by Termy »

Might be worth running type sudo instead, in-case there's an alias or function overriding it, which which(1) won't pick up.
I'm also Terminalforlife on GitHub.
willem_mint
Level 1
Level 1
Posts: 2
Joined: Fri Oct 21, 2022 6:00 am

Re: using sudo inserts '[' before command

Post by willem_mint »

Hi Karlchen and Termy,

Thank you for your replies.

I do have nala installed, which I obviously should have mentioned in my first post. Sorry, it had been a long day when I posted that.

If I use type sudo I do see a condition that reroutes apt to nala, but that doesn't appear to insert anything...

karlchen wrote: Fri Oct 21, 2022 6:26 am Hello, willem_mint.

Could you please share the complete terminal output of the commandline sudo mkdir test?

Code: Select all

$ sudo mkdir test

Opdracht '[mkdir' niet gevonden, bedoelde u wellicht:

  opdracht 'hmkdir' van deb hfsutils (3.2.6-14)
  opdracht 'mkdir' van deb coreutils (8.30-3ubuntu2)
  opdracht 'mmkdir' van deb mblaze (0.6-1)

Probeer: sudo apt install <deb name>

[sudo] wachtwoord voor fam: 

karlchen wrote: Fri Oct 21, 2022 6:26 am Could you also please share the output of this command: which sudo.

Code: Select all

$ which sudo
/usr/bin/sudo

Termy wrote: Fri Oct 21, 2022 9:02 am Might be worth running type sudo instead, in-case there's an alias or function overriding it, which which(1) won't pick up.

Code: Select all

sudo is een functie
sudo () 
{ 
    if ["$1" = "apt" ]; then
        shift;
        command sudo nala "$@";
    else
        command sudo "$@";
    fi
}

User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: using sudo inserts '[' before command

Post by Termy »

There we go. You have a function for sudo(8), and I see a typo in the code.

Replace:

Code: Select all

if ["$1" = "apt" ]; then
With:

Code: Select all

if [ "$1" = "apt" ]; then
Nice wee function though, especially by using the command builtin to avoid function recursion. The function is probably (but not necessarily) saved in '~/.bashrc', BTW.

An explanation for why you were getting that error: $1 (first positional parameter) would equal, in this case, mkdir, but you've prepended [ to mkdir, making [mkdir. Because of how if statements work in BASH, if is trying to test the exit status of an invalid and non-existent command [mkdir. Everything else on that line, after that word (shell's idea of a word), would be taken as arguments to the invalid command, hence you saw nothing about that, and the rest was likely ignored because of the syntax error. By adding the whitespace, BASH can now find a valid and existing mkdir(1) tool (usually in '/bin/mkdir'), allowing the statement to begin testing its exit status for the logic.
I'm also Terminalforlife on GitHub.
User avatar
karlchen
Level 23
Level 23
Posts: 18239
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: using sudo inserts '[' before command

Post by karlchen »

Great tip the "type" command, Termy. It was the right track indeed.
And good catch, spotting the missing blank inside the if clause, which in the corrected form reads if [ "$1" = "apt" ]; then
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 792 days now.
Lifeline
Locked

Return to “Other topics”