Shell
In this page will refer as shell to the device that allows to communicate with the system, including terminal,
shells and control characters (think about Ctrl-v
in the shell).
Normally in the interaction with a client you have to take into account
- the terminal
- the shell
- the application
so, if we want to rename a file named -|^C
to foo
you have to issue the following
keys sequence
m v Space . / - \ | Ctrl-V Ctrl-C Space f o o Return
into the terminal.
Take into account also the path separator and the syntax behind it.
Console&TTY
An useful diagram from here
An important feature of a shell like bash
is the stream redirection:
by default every process has associated at least three file descriptors
stdin
(associated with the literal number 0)stdout
(associated with 1)stderr
(associated with 2)
but it's possible to open/close old/new file descriptors directly from the command line using the following expressions
[n]>output
: redirect streamn
to fileoutput
that it's created if needed; the file is truncated[n]>>output
: as above but the content is appended[n]>&word
: duplicated output file descriptor
Note that the order of redirections is significant. For example, the command
ls > dirlist 2>&1
directs both standard output and standard error to the file dirlist, while the command
ls 2>&1 > dirlist
directs only the standard output to file dirlist, because the standard error
was duplicated from the standard output before the standard output was
redirected to dirlist. To understand better think of an analogy where >
is like
the equality operator =
and &
is instead used as a variable definition like $
https://unix.stackexchange.com/a/37662/32025
1 = /dev/tty
2 = /dev/tty
1> file.txt 2>&1
1 = file.txt
2 = $1 # and currently $1 = file.txt
1 = file.txt
2 = file.txt
2>&1 > file.txt
2 = $1 # and currently $1 = /dev/tty
1 = file.txt
1 = file.txt
2 = /dev/tty
So you need to think about &
as a copy of a file descriptor and not as a reference.
Links
- How to use arguments from previous command?
- Programming with ANSI escape codes
- The TTY demystified
- Abusing the FILE structure
- http://blog.hostilefork.com/where-printf-rubber-meets-road/
- https://sourceware.org/glibc/wiki/LibioVtables
- SO question and answer What are the responsibilities of each Pseudo-Terminal (PTY) component (software, master side, slave side)?
- !!Con West 2019 - Tabitha Sable: My, my, TTY!
Links
- dylanaraps/pure-bash-bible A collection of pure bash alternatives to external processes
- Google Bash style guide
- http://resources.mpi-inf.mpg.de/departments/rg1/teaching/unixffb-ss98/quoting-guide.html
- http://twistedoakstudios.com/blog/Post4872_dont-treat-paths-like-strings
- Why Bash is like that: suid
- buffering in standard streams
- Command Injection Without Spaces
- HINTS FOR WRITING UNIX TOOLS
- PIPES, FORKS, & DUPS: UNDERSTANDING COMMAND EXECUTION AND INPUT/OUTPUT DATA FLOW
- How fast are Linux pipes anyway?
- What exactly was the point of
[ “x$var” = “xval” ]
? - Bash Pitfalls