strs has more than 50 tools that make working with strings in the shell easier.
strs brings common string convenience methods to shells like Bash, because string manipulation in shells can be hard.
$ str capitalize "hey there! :fire:" | str to-emoji
Hey there! π₯
$ str repeat 5 β | str join π
β π β π β π β π βIf you're on Debian, you can use strs to take your apt sources from Debian testing, point them to Debian stable on the fly, and then send them to a stable machine:
$ str replace testing stable < sources.list | ssh host "cat > /etc/apt/sources.list"To do the same with sed, you'd need to know sed's regex syntax, if your sed comes with the -i feature flag, and if it's GNU sed or BSD sed.
strs, on the other hand, has a uniform interface and set of features across platforms, shells and operating systems, including Windows.
strs has string tools that are similar to those that are built into Bash, and it has commands for features that Bash doesn't have syntactic sugar for, as well.
The following examples of Bash code only work with Bash, whereas strs commands will work if you're using Bash, zsh, PowerShell or something else.
string='This is an example.'
$ echo "${#string}"
19$ str length "$string"
19Or, using pipes:
$ echo $string | str length
19front='This'
end='example.'
$ echo "${string#$front}" # from front
is an example.
$ echo "${string%$end}" # from end
This is an$ str lstrip $front "$string"
is an example.
$ str rstrip $end "$string"
This is an
$ str strip $front$end "$string"
is anOr, using pipes:
$ echo $string | str lstrip $front
is an example.
$ echo $string | str rstrip $end
This is an
$ echo $string | str strip $front$end
is an$ echo "${string^}" # capitalize first char
This is an example.
$ echo "${string^^}" # capitalize all
THIS IS AN EXAMPLE.
$ echo "${string,,}" # lower all
this is an example.$ str capitalize "$string"
This is an example.
$ str upper "$string"
THIS IS AN EXAMPLE.
$ str lower "$string"
this is an example.Or:
$ echo $string | str capitalize
This is an example.
$ echo $string | str upper
THIS IS AN EXAMPLE.
$ echo $string | str lower
this is an example.old='an'
new='a'
$ echo "${string//$old/$new}" # replace all
This is a example.
$ echo "${string/$old/$new}" # replace first
This is a example.$ str replace $old $new "$string"
This is a example.
$ str replace $old $new "$string" --count 1
This is a example.
$ str replace-first $old $new "$string"
This is a example.Or:
$ echo $string | str replace $old $new
$ echo $string | str replace $old $new --count 1
$ echo $string | str replace-first $old $newstrs has string manipulation commands that don't have syntactic sugar in Bash.
string='This is an example.'
$ str casefold "$string"
this is an example.$ echo $string | str casefold
this is an example.width=40
$ str center $width "$string"
This is an example. $ echo $string | str center $width
This is an example. countChar='e'
$ str count $countChar "$string"
2$ echo $string | str count $countChar
2find='e'
$ str find $find "$string"
11$ echo $string | str find $find
11$ str index $find "$string"
11$ echo $string | str index $find
11on='_'
$ str join $on $string
This_is_an_example.$ str split ' ' "$string" | str join $on
This_is_an_example.part=' '
$ str partition "$part" "$string"
This
is an example.$ echo $string | str partition "$part"
[...]split=' '
$ str split "$split" "$string"
This
is
an
example.$ echo $string | str split "$split"
[...]strip='.'
$ str strip $strip "$string"
This is an example$ echo $string | str strip $strip
This is an example$ str swapcase "$string"
tHIS IS AN EXAMPLE.$ echo $string | str swapcase
tHIS IS AN EXAMPLE.$ str title "$string"
This Is An Example.$ echo $string | str title
This Is An Example.$ str zfill $width "$string"
000000000000000000000This is an example.$ echo $string | str zfill $width
000000000000000000000This is an example.$ str repeat 3 "$string"
This is an example.
This is an example.
This is an example.$ echo $string | str repeat 3
[...]$ str ljust $width "$string" --fillchar '*'
This is an example.*********************$ echo $string | str ljust $width --fillchar '*'
This is an example.*********************$ str lstrip T "$string"
his is an example.$ echo $string | str lstrip T
his is an example. $ str rfind $find "$string"
17$ echo $string | str rfind $find
17$ str rindex $find "$string"
17$ echo $string | str rindex $find
17$ str rjust $width "$string"
This is an example.$ echo $string | str rjust $width
This is an example.remove='.'
$ str rstrip $remove "$string"
This is an example$ echo $string | str rstrip $remove
This is an example$ str rpartition "$part" "$string"
This is an
example.$ echo $string | str rpartition "$part"
[...]$ str rsplit "$split" "$string"
This
is
an
example.$ echo $string | str rsplit "$split"
[...]strs has tools that deal with UTF-8, ASCII and emojis, and it has tools that aren't found in Python or common shells.
$ str to-ascii "It is 20Β° Celsius outside."
It is 20deg Celsius outside.$ str to-ascii "Η Δ Η Η Η Δ Δ Η¦ Θ Η° Η¨ Δ½ Ε Ε Ε Ε€ Ε½"
A E I O U C D G H j K L N R S T Z$ str substring 3 "Hey there! π₯"
HeyYou can use negative indices like you can in Python:
$ str substring -3 "Hey there! π₯" --start 4
thereYou can use Python's slice syntax directly, too.
$ str slice 4:-3 "Hey there! π₯"
there$ str contains π₯ "Hey there! π₯"; echo $?
0$ str has-emoji "Hey there! π₯"; echo $?
0$ str from-emoji "Hey there! π₯"
Hey there! :fire:$ str col 2 'hello world'
world$ echo -e 'hello\tworld' | str col 2
world$ sudo dmesg | str nth 50
[73627.811739] Filesystems sync: 0.02 seconds$ str sbob "squidward likes krabby patties"
sQuIdWaRd LiKeS kRaBbY pAtTiEsstrs also brings Python's string validation methods to the shell.
Here's an example of how you'd use them in a conditional statement, followed by examples of other validation tools:
string='This is an example.'
if str startswith T "$string" && str endswith . "$string"; then
printf "Starts with T and ends with .\n"
elif str contains example "$string"; then
printf "Contains 'example'\n"
elif !str isalnum "$string"; then
printf "Isn't alphanumeric\n"
fi$ str startswith T "$string"; echo $?
0$ echo $string | str startswith T; echo $?
0$ str endswith . "$string"; echo $?
0$ echo $string | str endswith .; echo $?
0$ str isalnum "$string"; echo $?
0$ echo $string | str isalnum; echo $?
0$ str isalpha "$string"; echo $?
1$ echo $string | str isalpha; echo $?
1$ str isascii "$string"; echo $?
0$ echo $string | str isascii; echo $?
0$ str isdecimal "$string"; echo $?
1$ echo $string | str isdecimal; echo $?
1$ str isdigit "$string"; echo $?
1$ echo $string | str isdigit; echo $?
1$ str isidentifier "$string"; echo $?
1$ echo $string | str isidentifier; echo $?
1$ str islower "$string"; echo $?
1$ echo $string | str islower; echo $?
1$ str isnumeric "$string"; echo $?
1$ echo $string | str isnumeric; echo $?
1$ str isprintable "$string"; echo $?
0$ echo $string | str isprintable; echo $?
0$ str isspace "$string"; echo $?
1$ echo $string | str isspace; echo $?
1$ str istitle "$string"; echo $?
1$ echo $string | str istitle; echo $?
1$ str isupper "$string"; echo $?
1$ echo $string | str isupper; echo $?
1- A Unix shell like Bash, or PowerShell or Command Prompt on Windows
- Python 3.10+
requirements.txt
python3 -m pip install strsYou can view the strs package on PyPI.