115

You can change the here-doc operator to <<-. You can then indent both the here-doc and the delimiter with tabs: #! /bin/bash cat <<-EOF indented EOF echo Done Note that you must use tabs, not spaces to indent the here-doc. This means the above example won't work copied (Stack Exchange replaces tabs with spaces). There can not be any ...


95

There are multiple ways to do this. The simplest is probably this: cat <<EOF | sh touch somefile echo foo > somefile EOF Another, which is nicer syntax in my opinion: ( cat <<EOF touch somefile echo foo > somefile EOF ) | sh This works as well, but without the subshell: { cat <<EOF touch somefile echo foo > somefile EOF } | ...


25

In short, use: unquoted heredoc keywords, e.g., EOF regular dollar char for outer (i.e. local) variables, e.g., $FOO escaped dollar char for inner (i.e. remote) variables, e.g. \$BAR If you leave the heredoc keyword (i.e. EOF) unquoted then the heredoc body is processed locally, so that $FOO is expanded to foo and BAR is expanded to the empty string. ...


19

VALUE=<<PERSON some data PERSON echo "$VALUE" No output. A here-document is a redirection, you can't redirect into a variable. When the command line is parsed, redirections are handled in a separate step from variable assignments. Your command is therefore equivalent to (note the space) VALUE= <<PERSON some data PERSON That is, it assigns ...


18

You can do: cat /dev/fd/3 3<< E1 /dev/fd/4 4<< E2 foo E1 bar E2 There can be only one stdin, as there's only one file descriptor 0. cat << EOF eof EOF is short for: cat /dev/fd/0 0<< EOF eof EOF And: cat <<< foo is: cat /dev/fd/0 0<<< foo You have to make up your mind what to open on file descriptor 0. cat ...


17

Here-Document is a kind of shell redirection, so the shell will perform it as normal redirection, from beginning to the end (or from left to right, or order of appearance). This's defined by POSIX: If more than one redirection operator is specified with a command, the order of evaluation is from beginning to end. In your command, cat will perform > ...


16

If you put the code you want to execute in e.g. /opt/my_script.sh, it's very easy to do what you want: tmux new-session -d -s "myTempSession" /opt/my_script.sh This starts a new detached session, named "myTempSession", executing your script. You can later attach to it to check out what it's doing, by executing tmux attach-session -t myTempSession. That is ...


15

Drop the single quotes around the here-document limit string EOF to avoid suppressing parameter substitution. More details here. Especially take note of "Example 19-7. Parameter substitution turned off" and "Example 19-8. A script that generates another script".


14

You can tell GNU sed to read the script from standard input with -f -, -f meaning to read the script from a file, and - meaning standard input as is common with a lot of commands. sed -f - "$IN" > "$OUT" << SED_SCRIPT s/a/1/g s/test/full/g SED_SCRIPT POSIX sed also supports -f, but the use of - for standard input is not documented. In this ...


14

Following up on your previous question, it sounds like you want both some uninterpreted text and some interpreted text going into a file. In that case, use two different cats (or echos): cat > /test <<'EOF' some uninterpreted text $(pwd) not substituted: $1 EOF cat >> /test <<EOF but this will substitute: $1 EOF There's a couple of ...


12

The terminating here-doc word must be the only characters on the line: no indenting allowed. Also, use $() instead of backticks -- they are nestable. count=$(sqlplus -s $configuser/$configpass@$ORACLE_SID <<END set pagesize 0 feedback off verify off heading off echo off; select max(cust_id) from cutomers exit; END ) echo $count ...


12

You want the -a option to tee which appends rather than overwriting.


12

Well, let's find out: unset file cat >"$file" <<EOF this is not in ${file=./myfile} EOF bash: : No such file or directory Dang. I guess it must be doing the >"$file" part first then. But what if...? unset file <<EOF cat >"$file" this is in ${file=./myfile} EOF ...no error...? cat ./myfile this is in ./myfile As it appears, order ...


12

The read command reads from its standard input stream and assigns what's read to the variable file (it's a bit more compicated than that, see long discussion here). The standard input stream is coming from the here-document redirected into the loop after done. If not given data from anywhere, it will read from the terminal, interactively. In this case ...


11

What you have defined with set -u is force an exit with an error message, if attempted to use undefined variable (The set builtin command). The form of here-documents << with EOF is equivalent to as if double-quoting the words inside to allow the variables to be expanded (parameter expansion) by the shell (bash in this case; also subject to command ...


11

Because the variable isn't being set by your heredoc: $ VALUE=<<PERSON > { > "type": "account", > "customer_id": "1234", > "customer_email": "jim@gmail.com", > } > PERSON $ echo "$VALUE" $ If you want to use a heredoc to assign a value to a variable, you need something like: $ read -d '' -r VALUE <<...


10

This is just shell variable expansion by bash. In this context whatever is between the curly braces will be iterated and expanded into the expression. $ echo var{1,2,3,4} var1 var2 var3 var4 $ echo var{1..10} var1 var2 var3 var4 var5 var6 var7 var8 var9 var10


10

Here document by default subjects to shell expansions, precisely parameter expansion, command substitution, and arithmetic expansion. So variable (parameter) expansion is happening in your case -- the variable LOGFILE is being expanded in the current shell, and as the variable presumably does not exist hence null is being returned (and replaced) as the ...


9

EOJ needs to be fully left-justified, ie. no leading white-space, and no trailing space either. Also, you could/should (depending on your needs) write the first one as <<'EOJ' .. the quotes disable some shell expansion which can otherwise occur. From info bash Here Documents This type of redirection instructs the shell to read input ...


9

The line which ends the here document is \$fff From the man bash section on Here Documents: The format of here-documents is: <<[-]word here-document delimiter No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters ...


9

A here-document is a redirection of the form: <<[-]DELIMITER .... .... .... DELIMITER The optional - (inside the brackets above) changes the way the delimiter is matched and allows indenting each line inside the heredoc content, with tabulations (no spaces allowed). "Matched" means the delimiter is matched to the opener (as when ...


9

The image you posted shows carriage returns (CR in the image). This is a DOS-formatted text file. Use dos2unix or some similar utility to convert the file to a proper Unix text file. As per comment below: If you absolutely need to do development in Notepad++ on Windows (I would personally recommend doing the development on the same OS that you're ...


9

Yes, but you'd be using a an here-string rather than a here-document: cat >"$HOME/myRep/tiesto" <<<'tiesto' This will send the string tiesto to cat on its standard input, and it will write the string to the file $HOME/myRep/tiesto through a redirection of its standard output. Note that here-strings are not standard but are implemented by at ...


9

The documentation for -i states: The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution via the shell's -c option....


8

Either stop indenting the EOF, or use <<-EOF earlier and indent it using tabs.


8

An array could make the string parsing without the need for a temporal file. Don't forget to turn off globbing. set -f IFS=: Hosts=($HostFull) HostMain=${Hosts[0]} HostMid=${Hosts[1]} HostSub=${Hosts[2]} set +f


8

One option is to stick with bash arr=(a b c) x=$(IFS=,;printf "%s" "${arr[*]}") echo "$x" a,b,c Alternately arr=(a b c) printf -v x "%s," "${arr[@]}" x=${x%,} echo "$x" a,b,c With the second option, you can set the separator to ,(comma followed by space) instead of , alone printf -v x "%s, " "${arr[@]}" x=${x%, } echo "$x" a, b, c


8

cat << EOF >&2 ... EOF Or: cat >&2 << EOF ... EOF or: >&2 cat << EOF ... EOF Or: usage() { cat << EOF ... EOF } >&2 function usage { is the ksh syntax. It only makes sense in the AT&T implementations of ksh where functions defined that way behave differently. In other shells, that non-standard ...


8

On systems with /dev/fd/x: cat - << E0 /dev/fd/3 3<< E3 foo bar E0 bar baz E3 That is, open the two here documents on different file descriptors. If you open them both on fd 0, of course the last open overrides the previous ones. The above would be more useful with commands like paste though. Note that zsh has the MULT_IOS feature (enabled ...


8

Passing variables (environment variables) over ssh is possible but generally restricted. You need to tell the client to send them. For instance with OpenSSH, that's with: ssh -o SendEnv=parameter host cmd... But you also need the server the accept it (AcceptEnv configuration directive with OpenSSH). Accepting any variable is a big security risk so is ...


Only top voted, non community-wiki answers of a minimum length are eligible