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.
Asked and answered a million times before. Please put in some work and Google, there are so many documents online that go into detail on what shell scripting and Bash is.
A shell in Linux/Unix means something which takes as input the user specified
commands, executes them and gives us the output.
Every command recognized by the shell is actually a program whose executable is
stored in the folders like /bin.
Example: ls is a command which lists all the files in a folder.
So here we are using ls command to list out all the folders in the
root directory. (root directory is represented by / in bash,
so when we say, /bin, it means the bin directory in the root folder)
Code:
anisha@linux-uitj:~>ls/bin etc initrd.lz media proc selinux tmp vmlinuz
boot home lib mnt root srv usr
dev initrd.img lost+found opt sbin sys var
All the files in the below output are recognized by the shell as valid executables.
Example:
In the below example notice the ls command itself listed inside the /bin
folder as an executable.
Code:
anisha@linux-uitj:~>cd /bin
anisha@linux-uitj:/bin>ls
arch df gzip mkdir pwd sync
awk dmesg hostname mknod readlink tar
csh fgconsole loadkeys pidof setserial usleep
date fgrep loadunimap ping sh vi
dbus-cleanup-sockets fillup logger ping6 showconsolefont vim
dbus-daemon fsync login pkill showkey vim-normal
dbus-monitor fuser ls ps sleep ypdomainname
dbus-send gawk lsmod psfaddtable sort zcat
deallocvt gunzip md5sum psfxtable su
If the shell recognizes the commands, it executes its executable file.
The commands recognized by the shell usually call dozens of other functions dealing
directly with the Linux kernel.
It looks too simple??, to know which functions does ls call "internally" to get the list of the files, there is another command named 'strace':
The titles which you see in red colour below are actually the functions called by ls, internally.
Code:
anisha@linux-uitj:~>stracels
execve("/bin/ls", ["ls"], [/* 89 vars */]) = 0
brk(0) = 0x618000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa7724ef000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa7724ee000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib64/mpi/gcc/openmpi/lib64/tls/x86_64/librt.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/mpi/gcc/openmpi/lib64/tls/x86_64", 0x7fff75b561b0) = -1 ENOENT (No such file or directory)
open("/usr/lib64/mpi/gcc/openmpi/lib64/tls/librt.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
...
write(1, "alltray-0.7.4dev.tar.gz\t\t\t opera"..., 55alltray-0.7.4dev.tar.gz opera-10.62-6438.x86_64.rpm
) = 55
write(1, "aptoncd\t\t\t\t\t osm2pgsql\n", 23aptoncd osm2pgsql
) = 23 ...
anisha@linux-uitj:~>
Distribution: Slackware (mainly) and then a lot of others...
Posts: 855
Rep:
@Anisha: Do you think the OP even can _read_ the script. Heck, I gave up after the second line. I have however bookmarked the page and will read this later.
@OP: Bash scripting is something *nix folks do. It is actually used to scare newbies like you. Hang in there, you would eventually understand.
@Anisha: Do you think the OP even can _read_ the script. Heck, I gave up after the second line. I have however bookmarked the page and will read this later.
@OP: Bash scripting is something *nix folks do. It is actually used to scare newbies like you. Hang in there, you would eventually understand.
Hello,
Sorry to disagree with you but what Anisha posted was in my opinion a very clear and more then complete explanation of what Bash is, and all of that in easy to understand terms. Heck, it's a lot more then what I would post to explain something that's basic knowledge and easy to find on the internet (hence my previous post).
Making a statement like you do, bringing down the importance of Bash (and for what it's worth other shells), gives a very wrong image of Linux/Unix. How is OP to understand like you point out he eventually will, when you 'bash' a perfectly good basic explanation?
Eric, thank you, for the soothing words you wrote as a comment in the reputation column. I enjoyed them so much that I was forced to make this post, just to thank you.
No thanks necessary, in my opinion you did a great job with that post in very understandable words. Should be appreciated by all newbies looking for a simple explanation of something that can go to very high complexity. Thank you for taking the time to post it. I'm sure a lot of users will agree with me. Statements like SilverBack's one don't add to the valor of Linux at all.
But it still seems very odd and inappropriate to introduce a newbie to bash using strace...
Anyway, bash is the program that interprets the commands you type at a terminal, and a shell script is a file full of commands that can be run by the shell so you don't have to type them one by one.
Like Anisha said, commands are actaully executable programs in certain directories. But some are built into the shell.
The shell also has syntax for variables, if-then-else statements, wildcards, math, etc.
But is still seems very odd to introduce a newbie to bash using strace...
Hi,
I thought it was a nice touch using strace to show that it's not all that simple as just using ls but that there's a 'world' of complex things below what we use on a regular basis. Something about what most newbies don't even think about but can be very useful information. If you want to tame the beast it's best to understand it's nature
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.