2

I am relatively new to Linux, but my background is as a network monkey, so I'm used to the CLI.

I just installed Debian onto a Libre Renegade and that went quite smoothly. So far I have had to alter the the grub at /etc/default/grub in order to change the default resolution due to the hardware I am using.

I then created a new user account using this command:

useradd -m -p password thescaryjello 

After that I ran the following commands to create an admins group and add the new account to that group and the sudo group.

groupadd admins usermod -aG admins thescaryjello usermod -aG sudo thescaryjello 

However, when I log in as the new account I receive the following message :

-sh 29 [: tty: unexpected operator 

Additionally, it doesn't display the username or host name prior to the $.

Any advice as to what might have caused this or where I should look for more information in the system would be greatly appreciated. Any linux or best practices advice would be lovely. Thank you all in advance.

4
  • 3
    What does the man page for the useradd command say about the shell it assigns to the new user when you don't specify the shell through command-line options? Perhaps the Debian default shell doesn't understand part of the startup scripts created for the user thescaryjello and you need to change the shell parameter? Commented Aug 23 at 23:45
  • Great question. The flag -s specifies the shell to use and the default is a blank string when done via CLI. If it has a blank string the system uses the SHELL variable in /etc/default/useradd. Additionally, since I am not using the -D flag the command will utilize default variables from the system. Commented Aug 23 at 23:57
  • And on debian 12/bookworm (at least) OOTB /etc/default/useradd SHELL=/bin/sh which points to dash, and the default profile does not set PS1 for dash to include host/user (as it does for bash, which is used for root OOTB) Commented Aug 25 at 1:08
  • Did the username you used contain a space, or anything other that letters, numbers and the underscore '_' ? Commented Aug 25 at 14:50

1 Answer 1

10
-sh 29 [: tty: unexpected operator 

This has nothing to do with operator accounts: it is talking about a syntax error in a conditional expression. Unfortunately this is a bit obscure because of the history of the Bourne/POSIX shell.

Background

The classic way to make conditional constructs ("if this is true, do that") in a Bourne/POSIX-style shell is the if ... then ... [else ...] fi syntax. For example, to test if /usr/local/lib/someutility is executable before actually trying to run it, you would do something like this:

if [ -x /usr/local/lib/someutility ] then /usr/local/lib/someutility some-parameters else echo "Installation error: /usr/local/lib/someutility is not executable." >&2 exit 1 fi 

The plain if command does not actually know how to test if something is executable or not: it actually executes its arguments as a separate command, and checks if the return code is zero (= test successful/true) or not (= test failed/false).

The [ is actually a separate command: it has the alternate name test. Modern shells might implement it internally for better performance, but for POSIX compatibility, it must also exist as an executable binary at /bin/[ or /usr/bin/[. It even has a man page: try man [ or man test on your system.

The [ (or test) command interprets the arguments given to it, performs the requested test, and exits with a corresponding return code.

The ] is a bit of syntactic sugar for humans: the [ command might use it as an "end-of-arguments" marker, or just ignore it completely. The if command does not care about the square brackets being paired or not: all it cares about is the return code.

The actual answer

The error message seems to indicate that on line 29 of whatever login script it's executing (maybe /etc/profile, maybe some /etc/profile.d/*.sh file, maybe $HOME/.profile in the user's home directory) has an erroneus if [ ... construct that looks like this:

if [ tty <...something...> ] 

Here, the tty <...something...> ] part is passed as an argument to the [ command, but as you can see from man [, it does not know about a test named tty. The error message refers to the argument that would specify the test to do as an operator, in the sense of arithmetic or logical operators.

If the error message happens only with new user accounts, then the error might be in the template login script files provided for new user accounts: the templates are located in /etc/skel/ directory. See ls -la /etc/skel: remember that you'll need the -a option for ls to show any files whose names begin with a dot, like .profile.

I would assume that the intention was something along the lines of "do something that produces some output, but only if the session is actually connected to a (true or pseudo) TTY device", or in other words, seems like a regular interactive login session. This is a good practice on login scripts, as login scripts will also occasionally be called in non-interactive contexts, e.g. when using scp or rsync to transfer files between hosts. In such a situation, extra output might mess up the file transfer.

If that is the case, the tty command will produce a return code 0 if the session is connected to a TTY device, and 1 otherwise. So the [ command should not be used with it at all, and the correct syntax would be:

if tty -s then # do something that produces output fi 

But this is actually excessively backwards-compatible at the expense of functionality. In any POSIX-compatible shell, you should be able to do this instead:

case $- in *i*) # do something that produces output ;; esac 

This directly tests whether the shell session is actually interactive or not (which is what you want to know when deciding whether to display output or not) instead of trying to infer it indirectly from the presence/absence of a TTY device connection.

1
  • 2
    On debian 12/bookworm default newuser shell is /bin/sh->dash and /etc/profile lines 28-31 are: for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done. Note $i unquoted. Thus if something creates an entry in profile.d like tty foo this will execute [ -r tty foo ] and cause the error. Commented Aug 25 at 1:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.