I made some investigations. According to the source code of bash.
TL;DR
How is a non-interactive shell defined?
- lack of commands history
- jobs are not supported
- no line editing feature
- errors gets line number
- no prompt
- execution is stopped after first error
Does bash -c (without additional -i option) always create a
non-interactive shell?
Yes
Longer version.
This function is called when bash receives -c
option.
no_line_editing = 1
means that you can't edit your commands by using backspace.
bash_history_reinit (0);
disables the history and commands auto completion.
static void
init_noninteractive ()
{
#if defined (HISTORY)
bash_history_reinit (0);
#endif /* HISTORY */
interactive_shell = startup_state = interactive = 0;
expand_aliases = posixly_correct; /* XXX - was 0 not posixly_correct */
no_line_editing = 1;
#if defined (JOB_CONTROL)
/* Even if the shell is not interactive, enable job control if the -i or
-m option is supplied at startup. */
set_job_control (forced_interactive||jobs_m_flag);
#endif /* JOB_CONTROL */
}
Job control is disabled by default unless you force it with -m
/* We can only have job control if we are interactive unless we
force it. */
if (interactive == 0 && force == 0)
{
job_control = 0;
original_pgrp = NO_PID;
shell_tty = fileno (stderr);
}
Syntax error messages contains line numbers.
/* Report a syntax error with line numbers, etc.
Call here for recoverable errors. If you have a message to print,
then place it in MESSAGE, otherwise pass NULL and this will figure
out an appropriate message for you. */
static void
report_syntax_error (message)
char *message;
{
...
if (interactive == 0)
print_offending_line ();
...
}
Simple test
root@test:~# ;;
-bash: syntax error near unexpected token `;;'
root@test:~# bash -c ';;'
bash: -c: line 0: syntax error near unexpected token `;;'
bash: -c: line 0: `;;'
Prompt is not printed after command execution.
/* Issue a prompt, or prepare to issue a prompt when the next character
is read. */
static void
prompt_again ()
{
char *temp_prompt;
if (interactive == 0 || expanding_alias ()) /* XXX */
return;
Command execution is stopped after first error.
/* Parse error, maybe discard rest of stream if not interactive. */
if (interactive == 0)
EOF_Reached = EOF;
man bash | grep -C5 -i interactive
? – Lucas Apr 17 '16 at 20:48LESS=+/'An interactive shell' man bash
. I suggest that you read it. – Apr 17 '16 at 22:21