How to get value from file in sh? [duplicate]












2















This question already has an answer here:




  • How to read a file into a variable in shell?

    6 answers



  • Difference between sh and bash

    9 answers




Setup:



File a contains:



22


File b contains:



12


I have shell script 1.sh:



#!/bin/sh

a=$(< a)
b=$(< b)
echo $(($a*$b)) > c


The script should get values from file a and b, multiply them *, and save to file c.
However after setting permission $ chmod a+rx 1.sh and running it $ ./1.sh it returns an error:



./1.sh: 5: ./1.sh: arithmetic expression: expecting primary: "*"


This error occurs because the variables $a and $b doesn't get value form files a and b.




  • If I echo $a and echo $b it returns nothing;

  • If I define a=22 and b=12 values in the script it works;

  • I also tried other ways of getting contents of files like a=$(< 'a'), a=$(< "a"), a=$(< "~/a"), and even a=$(< cat a). None of those worked.


Plot Twist:



However, if I change shebang line to #!/bin/bash so that Bash shell is used - it works.



Question:



How to properly get data from file in sh?










share|improve this question













marked as duplicate by Benjamin W., not2qubit, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 at 5:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Add output of hexdump -C a and hexdump -C b to your question.
    – Cyrus
    Nov 22 at 18:41










  • $(< file) is a Bash extension. For POSIX sh, $(cat file) instead.
    – Benjamin W.
    Nov 22 at 19:01












  • shellcheck.net would tell you "Warning (SC2039): In POSIX sh, $(<file) to read files is undefined." if you used the #!/bin/sh shebang.
    – Benjamin W.
    Nov 22 at 19:02
















2















This question already has an answer here:




  • How to read a file into a variable in shell?

    6 answers



  • Difference between sh and bash

    9 answers




Setup:



File a contains:



22


File b contains:



12


I have shell script 1.sh:



#!/bin/sh

a=$(< a)
b=$(< b)
echo $(($a*$b)) > c


The script should get values from file a and b, multiply them *, and save to file c.
However after setting permission $ chmod a+rx 1.sh and running it $ ./1.sh it returns an error:



./1.sh: 5: ./1.sh: arithmetic expression: expecting primary: "*"


This error occurs because the variables $a and $b doesn't get value form files a and b.




  • If I echo $a and echo $b it returns nothing;

  • If I define a=22 and b=12 values in the script it works;

  • I also tried other ways of getting contents of files like a=$(< 'a'), a=$(< "a"), a=$(< "~/a"), and even a=$(< cat a). None of those worked.


Plot Twist:



However, if I change shebang line to #!/bin/bash so that Bash shell is used - it works.



Question:



How to properly get data from file in sh?










share|improve this question













marked as duplicate by Benjamin W., not2qubit, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 at 5:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Add output of hexdump -C a and hexdump -C b to your question.
    – Cyrus
    Nov 22 at 18:41










  • $(< file) is a Bash extension. For POSIX sh, $(cat file) instead.
    – Benjamin W.
    Nov 22 at 19:01












  • shellcheck.net would tell you "Warning (SC2039): In POSIX sh, $(<file) to read files is undefined." if you used the #!/bin/sh shebang.
    – Benjamin W.
    Nov 22 at 19:02














2












2








2








This question already has an answer here:




  • How to read a file into a variable in shell?

    6 answers



  • Difference between sh and bash

    9 answers




Setup:



File a contains:



22


File b contains:



12


I have shell script 1.sh:



#!/bin/sh

a=$(< a)
b=$(< b)
echo $(($a*$b)) > c


The script should get values from file a and b, multiply them *, and save to file c.
However after setting permission $ chmod a+rx 1.sh and running it $ ./1.sh it returns an error:



./1.sh: 5: ./1.sh: arithmetic expression: expecting primary: "*"


This error occurs because the variables $a and $b doesn't get value form files a and b.




  • If I echo $a and echo $b it returns nothing;

  • If I define a=22 and b=12 values in the script it works;

  • I also tried other ways of getting contents of files like a=$(< 'a'), a=$(< "a"), a=$(< "~/a"), and even a=$(< cat a). None of those worked.


Plot Twist:



However, if I change shebang line to #!/bin/bash so that Bash shell is used - it works.



Question:



How to properly get data from file in sh?










share|improve this question














This question already has an answer here:




  • How to read a file into a variable in shell?

    6 answers



  • Difference between sh and bash

    9 answers




Setup:



File a contains:



22


File b contains:



12


I have shell script 1.sh:



#!/bin/sh

a=$(< a)
b=$(< b)
echo $(($a*$b)) > c


The script should get values from file a and b, multiply them *, and save to file c.
However after setting permission $ chmod a+rx 1.sh and running it $ ./1.sh it returns an error:



./1.sh: 5: ./1.sh: arithmetic expression: expecting primary: "*"


This error occurs because the variables $a and $b doesn't get value form files a and b.




  • If I echo $a and echo $b it returns nothing;

  • If I define a=22 and b=12 values in the script it works;

  • I also tried other ways of getting contents of files like a=$(< 'a'), a=$(< "a"), a=$(< "~/a"), and even a=$(< cat a). None of those worked.


Plot Twist:



However, if I change shebang line to #!/bin/bash so that Bash shell is used - it works.



Question:



How to properly get data from file in sh?





This question already has an answer here:




  • How to read a file into a variable in shell?

    6 answers



  • Difference between sh and bash

    9 answers








bash unix sh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 18:37









Artūrs Laizāns

113




113




marked as duplicate by Benjamin W., not2qubit, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 at 5:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Benjamin W., not2qubit, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 at 5:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Add output of hexdump -C a and hexdump -C b to your question.
    – Cyrus
    Nov 22 at 18:41










  • $(< file) is a Bash extension. For POSIX sh, $(cat file) instead.
    – Benjamin W.
    Nov 22 at 19:01












  • shellcheck.net would tell you "Warning (SC2039): In POSIX sh, $(<file) to read files is undefined." if you used the #!/bin/sh shebang.
    – Benjamin W.
    Nov 22 at 19:02


















  • Add output of hexdump -C a and hexdump -C b to your question.
    – Cyrus
    Nov 22 at 18:41










  • $(< file) is a Bash extension. For POSIX sh, $(cat file) instead.
    – Benjamin W.
    Nov 22 at 19:01












  • shellcheck.net would tell you "Warning (SC2039): In POSIX sh, $(<file) to read files is undefined." if you used the #!/bin/sh shebang.
    – Benjamin W.
    Nov 22 at 19:02
















Add output of hexdump -C a and hexdump -C b to your question.
– Cyrus
Nov 22 at 18:41




Add output of hexdump -C a and hexdump -C b to your question.
– Cyrus
Nov 22 at 18:41












$(< file) is a Bash extension. For POSIX sh, $(cat file) instead.
– Benjamin W.
Nov 22 at 19:01






$(< file) is a Bash extension. For POSIX sh, $(cat file) instead.
– Benjamin W.
Nov 22 at 19:01














shellcheck.net would tell you "Warning (SC2039): In POSIX sh, $(<file) to read files is undefined." if you used the #!/bin/sh shebang.
– Benjamin W.
Nov 22 at 19:02




shellcheck.net would tell you "Warning (SC2039): In POSIX sh, $(<file) to read files is undefined." if you used the #!/bin/sh shebang.
– Benjamin W.
Nov 22 at 19:02












3 Answers
3






active

oldest

votes


















1














Ignore everything from file a and b but numbers:



#!/bin/sh

a=$(tr -cd 0-9 < a)
b=$(tr -cd 0-9 < b)
echo $(($a*$b))




See: man tr






share|improve this answer























  • Could also do just echo $((a*b)).
    – Benjamin W.
    Nov 22 at 19:12



















0














If you're looking for "true" Bourne-Shell compatibility, as opposed to Bash's emulation, then you have to go old school:



#!/bin/sh
a=`cat a`
b=`cat b`
expr $a * $b > c


I tried your original example under #!/bin/sh on both macOS and Linux (FC26), and it behaved properly, assuming a and b had UNIX line-endings. If that can't be guaranteed, and you need to run under #!/bin/sh (as emulated by bash), then something like this will work:



#!/bin/sh
a=$(<a)
b=$(<b)
echo $(( ${a%%[^0-9]*} * ${b%%[^0-9]*} )) > c





share|improve this answer



















  • 1




    Even the most ancient shells support command substitution with $(...) instead of backticks. You have to go back many decades to find one that doesn't. POSIX definitely supports it. On the other hand, even when /bin/sh is Bash, it does not support $(<a).
    – Benjamin W.
    Nov 23 at 3:16










  • To your first point, try /sbin/sh on Solaris 2.10. To your second point, try /bin/sh on Fedora 26.
    – DjPadz
    Nov 23 at 3:33





















0














There are many ways. One obvious way is to pipe in a sub-process by Command Substitution:



A=$(cat fileA.txt)   # 22
B=$(cat fileB.txt) # 12
echo $((A*B))
# <do it in your head!>


If there are any other problems with multiple lines, you need to look into how to use the Bash variable $IFS (Internal File Separator). Usually IFS is defined by: IFS=$' tn', so if you need to be able to reliably read lines endings from both Windows and Linux EOL's you may need to modify it.





ADDENDUM:



Process Substitution




Bash, Zsh, and AT&T ksh{88,93} (but not pdksh/mksh) support process
substitution. Process substitution isn't specified by POSIX. You may
use NamedPipes to accomplish the same things. Coprocesses can also do
everything process substitutions can, and are slightly more portable
(though the syntax for using them is not).




This also means that most Android OS does not allow process substitution, since their shells are most often based on mksh.



From man bash:



Process Substitution
Process substitution allows a process's input or output to be referred to using a filename. It takes the form of <(list) or >(list). The
process list is run asynchronously, and its input or output appears as a filename. This filename is passed as an argument to the current
command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form
is used, the file passed as an argument should be read to obtain the output of list. Process substitution is supported on systems that sup-
port named pipes (FIFOs) or the /dev/fd method of naming open files.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic
expansion.





share|improve this answer























  • Notice that process substitution (<(...)) isn't the same as command substitution ($(...)).
    – Benjamin W.
    Nov 23 at 3:10


















3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Ignore everything from file a and b but numbers:



#!/bin/sh

a=$(tr -cd 0-9 < a)
b=$(tr -cd 0-9 < b)
echo $(($a*$b))




See: man tr






share|improve this answer























  • Could also do just echo $((a*b)).
    – Benjamin W.
    Nov 22 at 19:12
















1














Ignore everything from file a and b but numbers:



#!/bin/sh

a=$(tr -cd 0-9 < a)
b=$(tr -cd 0-9 < b)
echo $(($a*$b))




See: man tr






share|improve this answer























  • Could also do just echo $((a*b)).
    – Benjamin W.
    Nov 22 at 19:12














1












1








1






Ignore everything from file a and b but numbers:



#!/bin/sh

a=$(tr -cd 0-9 < a)
b=$(tr -cd 0-9 < b)
echo $(($a*$b))




See: man tr






share|improve this answer














Ignore everything from file a and b but numbers:



#!/bin/sh

a=$(tr -cd 0-9 < a)
b=$(tr -cd 0-9 < b)
echo $(($a*$b))




See: man tr







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 18:51

























answered Nov 22 at 18:45









Cyrus

45.1k43676




45.1k43676












  • Could also do just echo $((a*b)).
    – Benjamin W.
    Nov 22 at 19:12


















  • Could also do just echo $((a*b)).
    – Benjamin W.
    Nov 22 at 19:12
















Could also do just echo $((a*b)).
– Benjamin W.
Nov 22 at 19:12




Could also do just echo $((a*b)).
– Benjamin W.
Nov 22 at 19:12













0














If you're looking for "true" Bourne-Shell compatibility, as opposed to Bash's emulation, then you have to go old school:



#!/bin/sh
a=`cat a`
b=`cat b`
expr $a * $b > c


I tried your original example under #!/bin/sh on both macOS and Linux (FC26), and it behaved properly, assuming a and b had UNIX line-endings. If that can't be guaranteed, and you need to run under #!/bin/sh (as emulated by bash), then something like this will work:



#!/bin/sh
a=$(<a)
b=$(<b)
echo $(( ${a%%[^0-9]*} * ${b%%[^0-9]*} )) > c





share|improve this answer



















  • 1




    Even the most ancient shells support command substitution with $(...) instead of backticks. You have to go back many decades to find one that doesn't. POSIX definitely supports it. On the other hand, even when /bin/sh is Bash, it does not support $(<a).
    – Benjamin W.
    Nov 23 at 3:16










  • To your first point, try /sbin/sh on Solaris 2.10. To your second point, try /bin/sh on Fedora 26.
    – DjPadz
    Nov 23 at 3:33


















0














If you're looking for "true" Bourne-Shell compatibility, as opposed to Bash's emulation, then you have to go old school:



#!/bin/sh
a=`cat a`
b=`cat b`
expr $a * $b > c


I tried your original example under #!/bin/sh on both macOS and Linux (FC26), and it behaved properly, assuming a and b had UNIX line-endings. If that can't be guaranteed, and you need to run under #!/bin/sh (as emulated by bash), then something like this will work:



#!/bin/sh
a=$(<a)
b=$(<b)
echo $(( ${a%%[^0-9]*} * ${b%%[^0-9]*} )) > c





share|improve this answer



















  • 1




    Even the most ancient shells support command substitution with $(...) instead of backticks. You have to go back many decades to find one that doesn't. POSIX definitely supports it. On the other hand, even when /bin/sh is Bash, it does not support $(<a).
    – Benjamin W.
    Nov 23 at 3:16










  • To your first point, try /sbin/sh on Solaris 2.10. To your second point, try /bin/sh on Fedora 26.
    – DjPadz
    Nov 23 at 3:33
















0












0








0






If you're looking for "true" Bourne-Shell compatibility, as opposed to Bash's emulation, then you have to go old school:



#!/bin/sh
a=`cat a`
b=`cat b`
expr $a * $b > c


I tried your original example under #!/bin/sh on both macOS and Linux (FC26), and it behaved properly, assuming a and b had UNIX line-endings. If that can't be guaranteed, and you need to run under #!/bin/sh (as emulated by bash), then something like this will work:



#!/bin/sh
a=$(<a)
b=$(<b)
echo $(( ${a%%[^0-9]*} * ${b%%[^0-9]*} )) > c





share|improve this answer














If you're looking for "true" Bourne-Shell compatibility, as opposed to Bash's emulation, then you have to go old school:



#!/bin/sh
a=`cat a`
b=`cat b`
expr $a * $b > c


I tried your original example under #!/bin/sh on both macOS and Linux (FC26), and it behaved properly, assuming a and b had UNIX line-endings. If that can't be guaranteed, and you need to run under #!/bin/sh (as emulated by bash), then something like this will work:



#!/bin/sh
a=$(<a)
b=$(<b)
echo $(( ${a%%[^0-9]*} * ${b%%[^0-9]*} )) > c






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 19:29

























answered Nov 22 at 19:23









DjPadz

3614




3614








  • 1




    Even the most ancient shells support command substitution with $(...) instead of backticks. You have to go back many decades to find one that doesn't. POSIX definitely supports it. On the other hand, even when /bin/sh is Bash, it does not support $(<a).
    – Benjamin W.
    Nov 23 at 3:16










  • To your first point, try /sbin/sh on Solaris 2.10. To your second point, try /bin/sh on Fedora 26.
    – DjPadz
    Nov 23 at 3:33
















  • 1




    Even the most ancient shells support command substitution with $(...) instead of backticks. You have to go back many decades to find one that doesn't. POSIX definitely supports it. On the other hand, even when /bin/sh is Bash, it does not support $(<a).
    – Benjamin W.
    Nov 23 at 3:16










  • To your first point, try /sbin/sh on Solaris 2.10. To your second point, try /bin/sh on Fedora 26.
    – DjPadz
    Nov 23 at 3:33










1




1




Even the most ancient shells support command substitution with $(...) instead of backticks. You have to go back many decades to find one that doesn't. POSIX definitely supports it. On the other hand, even when /bin/sh is Bash, it does not support $(<a).
– Benjamin W.
Nov 23 at 3:16




Even the most ancient shells support command substitution with $(...) instead of backticks. You have to go back many decades to find one that doesn't. POSIX definitely supports it. On the other hand, even when /bin/sh is Bash, it does not support $(<a).
– Benjamin W.
Nov 23 at 3:16












To your first point, try /sbin/sh on Solaris 2.10. To your second point, try /bin/sh on Fedora 26.
– DjPadz
Nov 23 at 3:33






To your first point, try /sbin/sh on Solaris 2.10. To your second point, try /bin/sh on Fedora 26.
– DjPadz
Nov 23 at 3:33













0














There are many ways. One obvious way is to pipe in a sub-process by Command Substitution:



A=$(cat fileA.txt)   # 22
B=$(cat fileB.txt) # 12
echo $((A*B))
# <do it in your head!>


If there are any other problems with multiple lines, you need to look into how to use the Bash variable $IFS (Internal File Separator). Usually IFS is defined by: IFS=$' tn', so if you need to be able to reliably read lines endings from both Windows and Linux EOL's you may need to modify it.





ADDENDUM:



Process Substitution




Bash, Zsh, and AT&T ksh{88,93} (but not pdksh/mksh) support process
substitution. Process substitution isn't specified by POSIX. You may
use NamedPipes to accomplish the same things. Coprocesses can also do
everything process substitutions can, and are slightly more portable
(though the syntax for using them is not).




This also means that most Android OS does not allow process substitution, since their shells are most often based on mksh.



From man bash:



Process Substitution
Process substitution allows a process's input or output to be referred to using a filename. It takes the form of <(list) or >(list). The
process list is run asynchronously, and its input or output appears as a filename. This filename is passed as an argument to the current
command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form
is used, the file passed as an argument should be read to obtain the output of list. Process substitution is supported on systems that sup-
port named pipes (FIFOs) or the /dev/fd method of naming open files.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic
expansion.





share|improve this answer























  • Notice that process substitution (<(...)) isn't the same as command substitution ($(...)).
    – Benjamin W.
    Nov 23 at 3:10
















0














There are many ways. One obvious way is to pipe in a sub-process by Command Substitution:



A=$(cat fileA.txt)   # 22
B=$(cat fileB.txt) # 12
echo $((A*B))
# <do it in your head!>


If there are any other problems with multiple lines, you need to look into how to use the Bash variable $IFS (Internal File Separator). Usually IFS is defined by: IFS=$' tn', so if you need to be able to reliably read lines endings from both Windows and Linux EOL's you may need to modify it.





ADDENDUM:



Process Substitution




Bash, Zsh, and AT&T ksh{88,93} (but not pdksh/mksh) support process
substitution. Process substitution isn't specified by POSIX. You may
use NamedPipes to accomplish the same things. Coprocesses can also do
everything process substitutions can, and are slightly more portable
(though the syntax for using them is not).




This also means that most Android OS does not allow process substitution, since their shells are most often based on mksh.



From man bash:



Process Substitution
Process substitution allows a process's input or output to be referred to using a filename. It takes the form of <(list) or >(list). The
process list is run asynchronously, and its input or output appears as a filename. This filename is passed as an argument to the current
command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form
is used, the file passed as an argument should be read to obtain the output of list. Process substitution is supported on systems that sup-
port named pipes (FIFOs) or the /dev/fd method of naming open files.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic
expansion.





share|improve this answer























  • Notice that process substitution (<(...)) isn't the same as command substitution ($(...)).
    – Benjamin W.
    Nov 23 at 3:10














0












0








0






There are many ways. One obvious way is to pipe in a sub-process by Command Substitution:



A=$(cat fileA.txt)   # 22
B=$(cat fileB.txt) # 12
echo $((A*B))
# <do it in your head!>


If there are any other problems with multiple lines, you need to look into how to use the Bash variable $IFS (Internal File Separator). Usually IFS is defined by: IFS=$' tn', so if you need to be able to reliably read lines endings from both Windows and Linux EOL's you may need to modify it.





ADDENDUM:



Process Substitution




Bash, Zsh, and AT&T ksh{88,93} (but not pdksh/mksh) support process
substitution. Process substitution isn't specified by POSIX. You may
use NamedPipes to accomplish the same things. Coprocesses can also do
everything process substitutions can, and are slightly more portable
(though the syntax for using them is not).




This also means that most Android OS does not allow process substitution, since their shells are most often based on mksh.



From man bash:



Process Substitution
Process substitution allows a process's input or output to be referred to using a filename. It takes the form of <(list) or >(list). The
process list is run asynchronously, and its input or output appears as a filename. This filename is passed as an argument to the current
command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form
is used, the file passed as an argument should be read to obtain the output of list. Process substitution is supported on systems that sup-
port named pipes (FIFOs) or the /dev/fd method of naming open files.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic
expansion.





share|improve this answer














There are many ways. One obvious way is to pipe in a sub-process by Command Substitution:



A=$(cat fileA.txt)   # 22
B=$(cat fileB.txt) # 12
echo $((A*B))
# <do it in your head!>


If there are any other problems with multiple lines, you need to look into how to use the Bash variable $IFS (Internal File Separator). Usually IFS is defined by: IFS=$' tn', so if you need to be able to reliably read lines endings from both Windows and Linux EOL's you may need to modify it.





ADDENDUM:



Process Substitution




Bash, Zsh, and AT&T ksh{88,93} (but not pdksh/mksh) support process
substitution. Process substitution isn't specified by POSIX. You may
use NamedPipes to accomplish the same things. Coprocesses can also do
everything process substitutions can, and are slightly more portable
(though the syntax for using them is not).




This also means that most Android OS does not allow process substitution, since their shells are most often based on mksh.



From man bash:



Process Substitution
Process substitution allows a process's input or output to be referred to using a filename. It takes the form of <(list) or >(list). The
process list is run asynchronously, and its input or output appears as a filename. This filename is passed as an argument to the current
command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form
is used, the file passed as an argument should be read to obtain the output of list. Process substitution is supported on systems that sup-
port named pipes (FIFOs) or the /dev/fd method of naming open files.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic
expansion.






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 1 at 9:07

























answered Nov 22 at 18:44









not2qubit

4,04813260




4,04813260












  • Notice that process substitution (<(...)) isn't the same as command substitution ($(...)).
    – Benjamin W.
    Nov 23 at 3:10


















  • Notice that process substitution (<(...)) isn't the same as command substitution ($(...)).
    – Benjamin W.
    Nov 23 at 3:10
















Notice that process substitution (<(...)) isn't the same as command substitution ($(...)).
– Benjamin W.
Nov 23 at 3:10




Notice that process substitution (<(...)) isn't the same as command substitution ($(...)).
– Benjamin W.
Nov 23 at 3:10



Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo