You are not logged in.

#1 Yesterday 14:44:58

system72
Member
Registered: 2025-11-22
Posts: 116
Website

[solved]installing arch with script

hi i have a script to install arch

https://git.system72.dev/dotfiles/tree/ … rchinstall

im experiencing a problem with heredocs, it doesnt let me assign variables inside of them, i was wondering if there was any alternatives or a solution to this prolbem. thanks.
p.s. im a shell scripting noob, also i dont know bash so the solutions please be posix.. thanks.

Last edited by system72 (Yesterday 18:53:57)

Offline

#2 Yesterday 15:21:02

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 434

Re: [solved]installing arch with script

system72 wrote:

im experiencing a problem with heredocs, it doesnt let me assign variables inside of them

What do you mean by "assign variables inside of heredoc"? Do you want to prevent variable expansion in generated content? You can escape dollar sign with backslash to do this, e.g.:

if [ "\${f}" != "/home/${username}/dotfiles/etc/default" ]; then
    cp -r "\${f}" /etc
fi

Offline

#3 Yesterday 15:22:32

system72
Member
Registered: 2025-11-22
Posts: 116
Website

Re: [solved]installing arch with script

no i mean like $f is not assigned to anything when i do it in the heredoc

its just empty

it tells me this

K4GJ.png

Last edited by system72 (Yesterday 15:24:46)

Offline

#4 Yesterday 15:52:21

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 434

Re: [solved]installing arch with script

system72 wrote:

no i mean like $f is not assigned to anything when i do it in the heredoc
its just empty

Because "${f}" is expanded by shell before arch-chroot run. There is no such variable assigned in your script, it expands to empty string.
If you want heredoc to contain "${f}" literally, you have to escape the dollar sign as shown above.

Offline

#5 Yesterday 15:56:35

system72
Member
Registered: 2025-11-22
Posts: 116
Website

Re: [solved]installing arch with script

so if i escape it then it will work

are there any alternatives to heredocs that are better for this usecase

Last edited by system72 (Yesterday 16:04:03)

Offline

#6 Yesterday 16:52:47

goeb
Member
Registered: 2015-06-03
Posts: 15

Re: [solved]installing arch with script

Just quote the EOF:

command <<"EOF"…

Offline

#7 Yesterday 17:08:02

system72
Member
Registered: 2025-11-22
Posts: 116
Website

Re: [solved]installing arch with script

ok thank you that seems better, are there any implicaitions of doing it that way that will mess with some other operations inside of the heredoc?

Offline

#8 Yesterday 17:31:11

goeb
Member
Registered: 2015-06-03
Posts: 15

Re: [solved]installing arch with script

Quoting the delimiter will prevent any shell expansion within the heredoc (variables, command substitution, etc.). If you want some things to be expanded and others to be literal, you'll have to escape individually. You can redirect it to a file instead of running it directly to check the resulting script for anything unexpected.

Offline

#9 Yesterday 18:21:44

system72
Member
Registered: 2025-11-22
Posts: 116
Website

Re: [solved]installing arch with script

im confused on what you mean can you give me some examples from my existing script. thanks.

Offline

#10 Yesterday 18:28:55

goeb
Member
Registered: 2015-06-03
Posts: 15

Re: [solved]installing arch with script

Try something like that:

var=123
cat <<EOF
var = $var
date = $( date )
math = $(( var * 2 ))
EOF

Unquoted, so stuff will be expanded. Run it with a quoted EOF for comparison. And if you want the literal date and the rest expanded, you need to manually escape as required:

var=123
cat <<EOF
var = \$var
date = $( date )
math = \$(( var * 2 ))
EOF

Offline

#11 Yesterday 18:38:43

system72
Member
Registered: 2025-11-22
Posts: 116
Website

Re: [solved]installing arch with script

thanks, as i understand it, if the EOF is quoted then all variables outside of the heredoc cannot be accessed inside of the heredoc but all variables inside of the heredoc get expanded correctly

so i should just escape manually right?

hm actually, it seems if the EOF is quoted you need to export the vars outside of the heredoc for them to be accessable. ok the quoting seems good for now. please correct me if im wrong

Last edited by system72 (Yesterday 18:43:06)

Offline

#12 Yesterday 18:50:34

goeb
Member
Registered: 2015-06-03
Posts: 15

Re: [solved]installing arch with script

Right. Think of it as a separate shell script (in the case of arch-chroot anyway, it just runs Bash if you don't give it a command to run, so you're basically feeding the heredoc to the bash command). And exported variables will be available in the script.

Offline

#13 Yesterday 18:53:43

system72
Member
Registered: 2025-11-22
Posts: 116
Website

Re: [solved]installing arch with script

thank you for your time

Offline

Board footer

Powered by FluxBB