What characters are valid in a here doc tag?

cat << 'what_characters_can_be_used_here'


I ask, because I want a string that is very unlikely. Thus, I want to include odd characters in the string. I use bash, and can use bash specific features, but it is interesting to know general POSIX limitations too. Also it is interesting to know the maximum safe length for the delimiter string.

  • What is your very unlikely string? – blissini Nov 19 '16 at 18:14
  • I would like to have a string of random ascii characters. However, it appears that bash accepts any unicode codepoint. – user877329 Nov 19 '16 at 18:17

According to the POSIX specification, the section on heredocs defines the heredoc "tag" simply as a word. This leads me to believe that anything that the shell defines as a valid word is acceptable.

As far as a maximum word length, I'd wager that depends on the shell you would be running the script on, though I very much doubt you would hit a hard limit on length without using an absurdly long string.

| improve this answer | |

You can use any non-empty sequence of characters other than a newline as the delimiter. POSIX doesn't state any limitation. You can specify a newline in the delimiter, but then it will never be found. Many shells support empty delimiters, but ATT ksh doesn't. In fact most shells accept arbitrary sequences of bytes other than null and newline, but I wouldn't recommend using invalid byte sequences. I also recommend not using carriage return characters as those may get reencoded away when the script is edited or transferred to another machine. Trailing whitespace is also prone to being accidentally removed during edition. Sticking to printable ASCII is safer.

Of course, if the delimuter includes a character that has a special meaning in shell syntax, you'll need to quote it after the << operator, and then you can only use a literal here-document, not a here-document with variable and command substitution.

| improve this answer | |
  • I do not want variable and command substitution in this case. – user877329 Nov 20 '16 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.