Redirection paths

The path that comes after the redirection operator can include directory parts, and tildes to expand; both of the following redirections will work, if the mydir directory exists in your home directory:

$ cd
$ printf 'Hello, file!\n' > mydir/myfile
$ printf 'Hello, file!\n' > ~/mydir/myfile

Variables such as HOME can also be expanded for the file destination:

$ printf 'Hello, file!\n' > "$HOME"/myfile

For output redirection to a new file path to work, you will need permission to create files in the destination directory. If the file already exists, you will need permission to write to it in order to replace its contents. For example, if we try to write a file to the root user's home directory while we are not root, we will ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.