H2ONaCl
H2ONaCl

Reputation: 11269

When does the slash in a Linux path need to be outside quotes in a bash script

Why does the first expansion not work, yet the second does?

I know tilde has to be expanded outside quotes but the slash also had to be outside, unexpectedly.

#!/bin/bash
ls ~"/Documents/bashscripts/test.sh"
ls ~/"Documents/bashscripts/test.sh"

Upvotes: 3

Views: 156

Answers (1)

kojiro
kojiro

Reputation: 77127

This is a subtlety in how tilde expansion works. In the second case, the tilde-followed-by-slash is expanded to the home directory of the current user. In the first case, the tilde-followed-by-quoted-word is attempted to be expanded to the home directory of the user named "/Documents/bashscripts/test.sh". From the manpage, Tilde Expansion section:

…all of the characters preceding the first unquoted slash are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. …

Upvotes: 6

Related Questions