Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) manual and the bash itself puzzle me on a number of issues.
REDIRECTION
Code:
$ : 7<test.txt # does nothing, it should have created /dev/fd/7
$ ll /dev/fd/
total 0
lrwx------. 1 user user 64 Jan 12 15:16 0 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:16 1 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:16 2 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:16 3 -> /proc/3401/fd
$ : {my}<test.txt # works, this was supposed to be a special case of the above
$ ll /dev/fd/
total 0
lrwx------. 1 user user 64 Jan 12 15:16 0 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:16 1 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:16 10 -> /home/user/tmp/test.txt
lrwx------. 1 user user 64 Jan 12 15:16 2 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:16 3 -> /proc/3402/fd
$ : 10<&- # does nothing, it was supposed to delete /dev/fd/10
$ ll /dev/fd/
total 0
lrwx------. 1 user user 64 Jan 12 15:17 0 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:17 1 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:17 10 -> /home/user/tmp/test.txt
lrwx------. 1 user user 64 Jan 12 15:17 2 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:17 3 -> /proc/3407/fd
$ : 11<&10- # deletes 10 as long as 11 does not exist, it was supposed to move 10 in 11
$ ll /dev/fd/
total 0
lrwx------. 1 user user 64 Jan 12 15:17 0 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:17 1 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:17 2 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:17 3 -> /proc/3408/fd
Simple Commands
Code:
$ a=1 echo $a # returns an empty line instead of '1'
Arrays
"Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts
with pathname expansion. "
Braces are required to include [subscript] in name. Conflicts with pathname expansion do not appear:
Code:
$ echo $my[1]
[1]
$ touch my1
$ echo $my[1]
[1]
Shell Variables
How can I verify the content of IFS? I'd like to see it.
Why does it not work as it was supposed to? Do I misread the manual? Is the manual inconsistent with the actual program?
How do I perform those tasks? How do I create a redirect to a file descriptor I choose, not to a file descriptor chosen by bash, bigger or equal to 10?
How do I close a file descriptor without using some number of a file descriptor which does not exist?
How do I duplicate a file descriptor in this version of bash?
I understand that bash can not be modified for backward compatibility reasons, but the manual certainly can be modified.
Your links lead to much clearer explanations than my bash manual, but still they do not clarify my questions. Maybe the features I can not find have just been removed from my bash?! That would be very bad for portability. And n<&- still does not work, as I show above. Does it work on your system? Is my example correct? I mean, was it supposed to work as expected? Maybe the file descriptor is closed but I still see it? No, it still works.
Regarding the /dev/fd/, this is highly dependend of the kernel version what happens there.
What matters is that
Code:
: 7<test.txt
has really temporary associated a descriptor 7 with the file test.txt.
That this is implemented by /dev/fd/7 is an assumption (mostly true), and that /dev/fd/7 remains is another assumption.
Code:
a=1 echo $a
does not work because $a is evaluated too early.
Also this is a temporary assignment.
The following works
Code:
a=1 sh
$ echo $a
$ exit
Or
Code:
a=1 sh -c 'echo $a'
Another demonstration
Code:
a=2 # the local shell variable
a=1 sh -c 'echo $a'
1
a=1 sh -c "echo $a"
2
Even within " " the shell substitutes $a with 2, then forks, the forked shell sets a=1 in its environment, then execs to sh -c ... and the sh prints the 2.
But in ' ' the shell does not substitute $a so the sh takes it from its environment.
Array handling denotes braces.
Code:
echo ${my[1]}
is the second index (starts with 0).
A demonstration
Code:
my=( first second )
echo "${my[1]} ${my[0]}"
echo "my has ${#my} elements, that are: ${my[@]}"
Last edited by MadeInGermany; 01-12-2018 at 11:21 AM.
if I understand well something like 7<test.txt is not specified at all, no such thing documented. That's why I asked where is it from?
It runs a command with file descriptor 7 redirected from the named file. In the case of the ":" (no-op) command, or any other command that is not going to read from FD 7, it is pointless. If you want that redirection to be available for the rest of the current script, you use the "exec" command:
Code:
exec 7<test.txt
Quote from the bash manpage:
"Redirecting Input
Redirection of input causes the file whose name results from the expansion of word to be opened for reading on file descriptor n, or the standard input (file descriptor 0) if n is not specified.
Regarding the /dev/fd/, this is highly dependend of the kernel version what happens there.
What matters is that
Code:
: 7<test.txt
has really temporary associated a descriptor 7 with the file test.txt.
That this is implemented by /dev/fd/7 is an assumption (mostly true), and that /dev/fd/7 remains is another assumption.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.