Questions tagged [shell-builtin]
A shell builtin is a command called from a shell, that is executed directly in the shell itself.
159
questions
-1
votes
1
answer
51
views
Is the following `readonly` use POSIX-ly correct?
I defined the following as read-only:
readonly root_command='sudo -s'
later used in my script as in:
exec $root_command
My question is, maybe I am slow or something, but I do not fully understand ...
0
votes
0
answers
23
views
How to interpret different behavior of sleep and wait when they are interrupted
I'm trying to understand the subtleties between sleep and sleep & wait.
If run the script test.sh
#!/bin/bash
sleep 10000
in one terminal and ps -aef --forest in another terminal, then I see
...
1
vote
1
answer
151
views
some_variable vs some-variable in zsh
Why is it that zsh seems to understand what to do with some_variable=1 command, but not some-variable=1 command? E.g.: $ fs_screen=1 mpv --fs-screen=${fs_screen} someFile.mp4 works fine but $ fs-...
0
votes
2
answers
94
views
arbitrary base conversion from base 10 using only builtins in bash
I'm trying to sort out a bash command to convert a number from base 10 to an arbitrary base, using a specified set of characters (for example, to base 26 using letters a-z, although that's not ...
1
vote
0
answers
50
views
Why is `popd` popping the wrong directory?
Is this expected behavior from popd?
>>> dirs -v
0 dirA
1 dirB
>>> dirs +0
dirA
>>> popd -n +0
dirA
>>> dirs -v
0 dirA
I would expect the last line to tell me that ...
-1
votes
1
answer
64
views
Why do the [ -z ] and the [ -n ] flags both return true when used without an expression or when testing an unset variable? [duplicate]
From the man test pages:
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
Executing [ -n $foo ], [ -n ], [ -z $foo ], [ -z ], ...
0
votes
1
answer
67
views
Problem with bash builtin bind and escaping
I'm trying to add a history fuzzyfind hotkey to my bash shell. However, my intended command gets cut in the middle:
[april@Capybara-2:~]$ cat ~/.bashrc
bind "\"\C-r\": \"\$(...
32
votes
1
answer
3k
views
How to get the cd shell-builtin to stop guessing?
For example:
$ ls -aF
./ ../ bin/
$ cd tin # with a tee, not bee
bin
$ pwd
/home/user/bin
In other words, cd guesses that what I really meant was cd bin, and successfully (huh?) changes the current ...
2
votes
1
answer
130
views
Using set -e (errexit) with block of commands and executing another on fail of that block (SC2181)
I just found out about set -e as I was searching for an answer to "how to run many commands (without &&) and immediately stop on non-zero exit code of any of them?". The set -e was ...
0
votes
1
answer
77
views
Why do some shell commands which are shell functions not always available?
I have noticed that using zsh the history command is not available when executed from Emacs via shell-command.
history | grep runserver results in zsh:fc:1: no such event: 1.
I also use the histdb ...
1
vote
1
answer
96
views
Why does `print -c` fail, but piping to `column` succeed to print columns?
This works:
#!/bin/zsh
### List apps ###
arch="$(paru --query --quiet --explicit --unrequired)"
repos=( $HOME/repos/./* )
npm=( $(npm list --global --parseable) )
box="${(l:20::─:)}&...
1
vote
2
answers
195
views
How can one modify the named directory hash table by "creating appropriate shell parameters"?
I'm used to add entrys to zshs named driectory hash table, by using hash -d <name>=<value>. Recently, I read the zshbuiltins(1) man page, where I found the following (description of the ...
0
votes
2
answers
456
views
Cannot Display Bash Functions within FZF Preview Window
How do I get the FZF Preview Window to Display Functions from my Current Bash Environment?
I want to list my custom bash functions using FZF, and view the code of a selected function in the FZF ...
3
votes
2
answers
148
views
Exporting zsh history separated by ‘\0’ characters instead of ‘\n’
I want to access zsh history with entries separated out by \0 instead of \n. The built-in parser can clearly handle multiline entries, because fc 123 brings up an editor with newlines preserved. ...
0
votes
0
answers
254
views
/usr/bin/type vs. bash's built-in type command
I was reading an article on various Linux commands that can show location of programs in the search path, including which, type, and whereis. The article says that type is a built-in bash command, and ...
2
votes
1
answer
459
views
About Precommand Modifiers in zsh
Background
While the zsh documentation (ch 6.2) provides a description for what precommand modifiers can do, it fails to provide examples for each type: -, builtin, command, exec, nocorrect, and ...
2
votes
2
answers
289
views
What is the zsh equivalent of "pushd -n" in bash?
I want to push a directory onto the directory stack in order to refer to it using "tilde shorthand" (eg. ~1 refers to the second entry in the directory list), but I don't want to actually ...
1
vote
1
answer
873
views
Is there a way to print the commands that the shell builtin `source` is calling when sourcing a script? [duplicate]
sh, bash, etc. have a -x flag that will show you each command that is executing in a script. This is handy for debugging.
$ sh -x my_script.sh
+ case $SOMETHING_DIR in
+ unset ...
-4
votes
1
answer
3k
views
How to resolve this error for 'help' command not found in Kali Linux? [closed]
I know that help is a shell built-in internal command used to find about built in commands such as cd. But it is not working in my Kali Linux terminal in VirtualBox. It is showing the following error:
...
6
votes
3
answers
1k
views
checkbashisms: what's wrong with `type'?
#!/bin/sh
foo() {
echo "in foo"
}
type foo
checkbashisms.pl obviously does not like type, why?
$ checkbashisms.pl foo.sh
possible bashism in foo.sh line 7(type):
type foo
Is it not ...
4
votes
2
answers
853
views
Bash's read builtin errors on a string-based timeout option specification but not an array-based one. Why?
In reading through the source to fff to learn more about Bash programming, I saw a timeout option passed to read as an array here:
read "${read_flags[@]}" -srn 1 && key "$REPLY&...
1
vote
1
answer
568
views
zsh: Halt when command not found
I want to stop the zsh script running when it encounters a command not found error.
I know of command_not_found_handler, but this function only allows me to print a message, not halt the parent shell.
...
6
votes
1
answer
400
views
Difference between command `command` vs command `builtin` in Shell Scripting
I understand the command command is specified in the latest POSIX standard and builtin is not. I also realize that both commands are regular builtins (i.e. they can be overwritten by user-defined ...
2
votes
1
answer
2k
views
How to detect EOF on a BASH script's stdin?
I have a bash function inside a script that needs to read data from stdin in fixed size blocks and send those, one at a time, to external programs for further processing. The function itself should ...
4
votes
1
answer
884
views
Are there any caveats in using shopt -s autocd?
I have recently discovered the feature shopt -s autocd:
autocd If set, a command name that is the name
of a directory is executed as if it were
the ...
0
votes
1
answer
140
views
What is the intended purpose of the () syntax in a bash function?
I am freshening up my bash skills. I use the term skills very loosely; I've been outta the game a long time. So, as a way to brush up, I decided to code something utilizing bash/zenity/yad. I read man ...
7
votes
5
answers
2k
views
IFS=',' /usr/bin/read vs IFS=',' read
Context
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/...
1
vote
1
answer
1k
views
replace space with underscore in expect script
#!/usr/bin/expect -f
set cmd "abc def ghi lkj"
set log [ expr echo ${cmd// /_} ]
I would like to log variable as ghi_lkj
first I wast trying to replace all spaces with underscores and then ...
8
votes
1
answer
1k
views
Is there a command to get builtin commands on zsh?
Is there a command to get builtin commands on zsh? For example, it is possible to get all builtin commands with the compgen -b command in the bash shell.
31
votes
5
answers
7k
views
Can I make cd be local to a function?
Is it possible to make a function like
function doStuffAt {
cd $1
# do stuff
}
but make it so invoking that function doesn't actually change my pwd, it just changes it for duration of the ...
8
votes
2
answers
2k
views
Bash autocomplete: List files and directories from other directory
I wrote a script called prpsls which creates or edits files inside the directory ~/proposals.
Now I'm writing the autocomplete script for it, and I'm trying to set it up in a way that, after I hit Tab,...
4
votes
1
answer
524
views
zsh: Read from stdin until a string delimiter
I want to read from stdin until a string delimiter MARKER=$'\0'"BRISH_MARKER" is encountered. I tried:
❯ unset br ; print -rn -- hi${MARKER}world | { IFS= read -d "$MARKER" -r ...
0
votes
1
answer
160
views
Bash history wrap around to the earliest, oldest command
How do we have bash history wrap around to the first, oldest command when at the latest, newest command?
1
vote
1
answer
365
views
Why print command can not be called with sudo? [duplicate]
% print ok
ok
% sudo print ok
sudo: print: command not found
It seems the print are not properly loaded. So what happened and how can I fix it?
0
votes
1
answer
112
views
Help with .bash program
I need to add a part to a shell script that will count down the number of command line arguments, printing one per second to the screen
and reporting how many command line arguments are left
(e.g., by ...
2
votes
3
answers
423
views
Reversing a file line-wise and character-wise
Input:
hello
enrico
output:
ocirne
olleh
To do this, I can simply tac a file and pipe the output to rev (or the other way around), so one function that does the job is just this:
revtac() { tac &...
-1
votes
1
answer
261
views
How to view all the list that can be viewed by update-alternatives?
Tabbing gives me ls data but i want something to be shown like x-session-manager , x-terminal-emulator and so on. How can i get list what can i set?
4
votes
2
answers
1k
views
Input redirection to for and while
Just out of curiosity,
This command works:
$ <file cat
But neither of these do:
$ <file for
bash: for: command not found...
$ <file while
bash: while: command not found...
Why is that?
0
votes
0
answers
22
views
'compgen' appears to have undocumented parameter '--', can I rely on it being present across systems? [duplicate]
I happen to be on Ubuntu 18.04, with GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
The output of
% compgen -A file --
test-data
foobar
--
outputs all files/dirs in the current directory ...
4
votes
1
answer
1k
views
How to build loadable builtins for Bash
As I recently learned, it's possible to dynamically load custom builtins into Bash (see enable in the manual, and this answer). However I'm not sure how to take advantage of this feature.
The linked ...
1
vote
2
answers
130
views
issues Debugging cd on custom shell [closed]
I am having issues with my custom shell cd command I created. Right now I am testing the base case where cd is the only argument, when that is the case its suppose to change the default directory to ...
-1
votes
1
answer
203
views
Manual entry for history missing... `man history` [duplicate]
I'm trying to figure out what's going on with my history... but I can't find the manual
history -h returns nothing man history returns a level 8 history manual, not the bash one we all use.
2
votes
1
answer
100
views
Why can't I see the manual for > but can see it for [?
I just did man [ and it opened the manual.
Then I tried doing man > but I get this error:
-bash: syntax error near unexpected token `newline'
How is > different from [?
0
votes
0
answers
149
views
Where is the information of the hash table stored? In other words, where is its database?
The "command hashing system" should store the path of the shell commands somewhere, right ? Is it a database or what? I am wondering where that is. Also, Is it encrypted ?
Do we have a way of ...
2
votes
1
answer
273
views
Use bash builtin `bind` command when starting chroot
I have a chroot and I want the chroot to have its own .inputrc file when it starts, and then run a program.
I'm used to starting the chroot with chroot <PATH> <PROGRAM_TO_RUN> so I tried
...
1
vote
1
answer
589
views
print list of environment variable names (without values)
There is a POSIX version of this question elsewhere; for those that don't want to live in a cave, this is the modern version.
15
votes
5
answers
3k
views
How to run a command 1 out of N times in Bash
I want a way to run a command randomly, say 1 out of 10 times. Is there a builtin or GNU coreutil to do this, ideally something like:
chance 10 && do_stuff
where do_stuff is only executed 1 ...
2
votes
0
answers
337
views
What is the difference between test, [, and [[ in terms of binary vs. builtin? [duplicate]
It is often written:
/usr/bin/test is a synonym for /usr/bin/[
[[ is a bash improvement for [
test and [ are also builtins
I understand the difference between [ and [[ but I am confused with the ...
1
vote
2
answers
201
views
Why does the command 'source' have that name?
I wonder why a command that executes commands from a file in the current shell is named source. I can't see a relation between run commands in the current shell and the meaning of the english word ...
0
votes
1
answer
268
views
How to know from which bash version a built-in is made available?
Is it possible to know from which specific version of bash a particular built-in is made available from the command-line? Is it only possible by browsing through release notes for each of the versions ...