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.
A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell's arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameter N may be referenced as ${N}, or as $N when N consists of a single digit. Positional parameters may not be assigned to with assignment statements. The set and shift builtins are used to set and unset them (see Shell Builtin Commands). The positional parameters are temporarily replaced when a shell function is executed (see Shell Functions).
When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces.
3.4.2 Special Parameters
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
*
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
@
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" .... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
#
Expands to the number of positional parameters in decimal.
?
Expands to the exit status of the most recently executed foreground pipeline.
-
(A hyphen.) Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).
$
Expands to the process id of the shell. In a () subshell, it expands to the process id of the invoking shell, not the subshell.
!
Expands to the process id of the most recently executed background (asynchronous) command.
0
Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands (see Shell Scripts), $0 is set to the name of that file. If Bash is started with the -c option (see Invoking Bash), then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the filename used to invoke Bash, as given by argument zero.
_
(An underscore.) At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.
...
3.5.3 Shell Parameter Expansion
The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.
When braces are used, the matching ending brace is the first ‘}’ not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.
The basic form of parameter expansion is ${parameter}. The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character that is not to be interpreted as part of its name.
If the first character of parameter is an exclamation point, a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
When not performing substring expansion, using the form described below, Bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter's existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
....
You should look at the 'Bash Reference Manual' for detailed explaination(s) that are complete.
To learn shell scripting, first you have to read a lot. The $ sign and the brackets {} are the basics to manage variables and their values in shell programming.
From the Advanced Bash Scripting Guide, chapter 4.1 (variable substitution)
Code:
The name of a variable is a placeholder for its value, the data it holds.
Referencing (retrieving) its value is called variable substitution.
$
Let us carefully distinguish between the name of a variable and its value.
If variable1 is the name of a variable, then $variable1 is a reference to its
value, the data item it contains.
And from chapter 9.3 (parameter substitution)
Code:
${parameter}
Same as $parameter, i.e., value of the variable parameter. In certain contexts, only
the less ambiguous ${parameter} form works.
May be used for concatenating variables with strings.
Indeed, the "advanced bash" is a bit... advanced, but you can take it as a good reference manual. Other texts are available on-line for beginners (look for Rute and for the Bash Guide for Beginners, just to cite two of them).
Distribution: @work:RHEL 5.4/Fedora 13, @home:slack64-current,ubuntu lynx studio
Posts: 65
Rep:
The ${} allows you to reference the value of the variable $DST within the literal string. This is only required if there is an alpha numeric character immediately after your variable in the literal string. e.g
brendan@ubuntu:~$ DST="Some text"
brendan@ubuntu:~$ echo "$DST followed by some more text"
Some text followed by some more text
brendan@ubuntu:~$ echo "$DSTimmediately followed by some more text"
followed by some more text
brendan@ubuntu:~$ echo "${DST}immediately followed by some more text"
Some textimmediately followed by some more text
The second echo command is trying to reference the value of variable $DSTimmediately rather than $DST.
Old bourne-based shells could not access positional parmaters beyond $9; accessing them required shifting them to lower numbers; perhaps the requirement to use {} with positional parameters of more than one digit was an interim solution for that.
I am working on my first shell script and i need som help please
I have a folder that i am sharing between my linux server and my windows computer at home with samba.
I want that folder not to take up more than 120 gb of my disk space.
Can anyone help me on the way how do i accomplish this by a shell script.
This is my script so far
#!/bin/bash
echo "The disk space of the directory blabla"
du -h /home/shane/blabla*
What i realise when i excecutes the script is, that it shows me the file size name of every files in that directory.
Can someone tell me how do i just get the total disk space that is used, i don't want to see whats in the directory only
the disc space that is used so far..?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.