comparison src/learn_bash.html.luan @ 47:84dd3edd03e9

learn_bash work
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 06 Jan 2024 21:28:44 -0700
parents 89fdc29b296f
children 889e3c2d2699
comparison
equal deleted inserted replaced
46:89fdc29b296f 47:84dd3edd03e9
369 } 369 }
370 files = { 370 files = {
371 title = [[Working with Files]] 371 title = [[Working with Files]]
372 content = function() 372 content = function()
373 %> 373 %>
374 <p></p>
375 <code block> 374 <code block>
376 ~/learn $ ls -F 375 ~/learn $ ls -F
377 dir1/ file1 file2 file3 376 dir1/ file1 file2 file3
378 ~/learn $ cp file1 copied 377 ~/learn $ cp file1 copied
379 ~/learn $ ls -F 378 ~/learn $ ls -F
405 ~/learn $ rm -d dir2 404 ~/learn $ rm -d dir2
406 ~/learn $ ls -F 405 ~/learn $ ls -F
407 dir1/ file1 file2 file3 406 dir1/ file1 file2 file3
408 </code> 407 </code>
409 408
410 <p></p>
411
412 <code block> 409 <code block>
413 ~/learn $ ls -F 410 ~/learn $ ls -F
414 dir1/ file1 file2 file3 411 dir1/ file1 file2 file3
415 ~/learn $ mkdir dir2 412 ~/learn $ mkdir dir2
416 ~/learn $ touch dir2/d2file 413 ~/learn $ touch dir2/d2file
417 ~/learn $ ls -F 414 ~/learn $ ls -F
418 dir1/ dir2/ file1 file2 file3 415 dir1/ dir2/ file1 file2 file3
419 ~/learn $ rm -R dir2 416 ~/learn $ rm -r dir2
420 ~/learn $ ls -F 417 ~/learn $ ls -F
421 dir1/ file1 file2 file3 418 dir1/ file1 file2 file3
422 </code> 419 </code>
423
424 <p></p>
425 420
426 <code block> 421 <code block>
427 ~/learn $ ls -F 422 ~/learn $ ls -F
428 dir1/ file1 file2 file3 423 dir1/ file1 file2 file3
429 ~/learn $ cp dir1 dir2 424 ~/learn $ cp dir1 dir2
430 cp: dir1 is a directory (not copied). 425 cp: dir1 is a directory (not copied).
431 ~/learn $ cp -R dir1 dir2 426 ~/learn $ cp -r dir1 dir2
432 ~/learn $ ls -F 427 ~/learn $ ls -F
433 dir1/ dir2/ file1 file2 file3 428 dir1/ dir2/ file1 file2 file3
434 ~/learn $ ls dir2 429 ~/learn $ ls dir2
435 d1file 430 d1file
436 ~/learn $ cp f* dir2 431 ~/learn $ cp f* dir2
437 ~/learn $ ls dir2 432 ~/learn $ ls dir2
438 d1file file1 file2 file3 433 d1file file1 file2 file3
439 ~/learn $ rm -R dir2 434 ~/learn $ rm -r dir2
440 ~/learn $ ls -F 435 ~/learn $ ls -F
441 dir1/ file1 file2 file3 436 dir1/ file1 file2 file3
442 </code> 437 </code>
443
444 <p></p>
445 438
446 <code block> 439 <code block>
447 ~/learn $ ls -F 440 ~/learn $ ls -F
448 dir1/ file1 file2 file3 441 dir1/ file1 file2 file3
449 ~/learn $ mkdir dir2 442 ~/learn $ mkdir dir2
450 ~/learn $ cp -R dir1 dir2 443 ~/learn $ cp -r dir1 dir2
451 ~/learn $ ls -F dir2 444 ~/learn $ ls -F dir2
452 dir1/ 445 dir1/
453 ~/learn $ ls -F dir2/dir1 446 ~/learn $ ls -F dir2/dir1
454 d1file 447 d1file
455 ~/learn $ rm -R dir2 448 ~/learn $ rm -r dir2
456 ~/learn $ ls -F 449 ~/learn $ ls -F
457 dir1/ file1 file2 file3 450 dir1/ file1 file2 file3
458 </code> 451 </code>
459 452
460 <p>I could explain all this, but I won't. You should learn to understand commands and their options using <code>man</code> and by playing with them. Don't continue until you completely understand the above.</p> 453 <p>I could explain all this, but I won't. You should learn to understand commands and their options using <code>man</code> and by playing with them. Don't continue until you completely understand the above.</p>
463 } 456 }
464 quote = { 457 quote = {
465 title = [[Quoting]] 458 title = [[Quoting]]
466 content = function() 459 content = function()
467 %> 460 %>
468 <p></p>
469 461
470 <code block> 462 <code block>
471 ~/learn $ echo a b 463 ~/learn $ echo a b
472 a b 464 a b
473 ~/learn $ echo "a b" 465 ~/learn $ echo "a b"
492 } 484 }
493 vars = { 485 vars = {
494 title = [[Variables]] 486 title = [[Variables]]
495 content = function() 487 content = function()
496 %> 488 %>
497 <p></p>
498 489
499 <code block> 490 <code block>
500 ~/learn $ echo $X 491 ~/learn $ echo $X
501 492
502 ~/learn $ X="some text" 493 ~/learn $ X="some text"
510 ~/learn $ echo $X 501 ~/learn $ echo $X
511 some text and more 502 some text and more
512 </code> 503 </code>
513 504
514 <p>Here <code>X</code> is a variable. You get its value with <code>$X</code>. This also works inside double-quotes but not inside single-quotes.</p> 505 <p>Here <code>X</code> is a variable. You get its value with <code>$X</code>. This also works inside double-quotes but not inside single-quotes.</p>
506
507 <p>There are special variables called environment variables that are used by Bash.</p>
508
509 <code block>
510 ~/learn $ echo $PATH
511 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/fschmidt/Dropbox/bin:/Users/fschmidt/hg/luan/scripts:/usr/local/opt/postgresql@9.5/bin
512 ~/learn $ which ls
513 /bin/ls
514 ~/learn $ cd /bin
515 /bin $ pwd
516 /bin
517 /bin $ ls
518 [ dd launchctl pwd test
519 bash df link rm unlink
520 cat echo ln rmdir wait4path
521 chmod ed ls sh zsh
522 cp expr mkdir sleep
523 csh hostname mv stty
524 dash kill pax sync
525 date ksh ps tcsh
526 /bin $ ls -F
527 [* dd* launchctl* pwd* test*
528 bash* df* link* rm* unlink*
529 cat* echo* ln* rmdir* wait4path*
530 chmod* ed* ls* sh* zsh*
531 cp* expr* mkdir* sleep*
532 csh* hostname* mv* stty*
533 dash* kill* pax* sync*
534 date* ksh* ps* tcsh*
535 /bin $ cd ~/learn
536 ~/learn $
537 </code>
538
539 <p><code>PATH</code> is an environment variable containing a list of directories separated by <code>:</code> that are searched for commands by Bash. The <code>which</code> command shows the full path to a command. <code>ls -F</code> appends a <code>*</code> to executable commands.</p>
540
541 <code block>
542 ~/learn $ subl file1
543 -bash: subl: command not found
544 ~/learn $ "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" file1
545 ~/learn $ PATH="$PATH:/Applications/Sublime Text.app/Contents/SharedSupport/bin"
546 ~/learn $ echo $PATH
547 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/fschmidt/Dropbox/bin:/Users/fschmidt/hg/luan/scripts:/usr/local/opt/postgresql@9.5/bin:/Applications/Sublime Text.app/Contents/SharedSupport/bin
548 ~/learn $ subl file1
549 ~/learn $
550 </code>
551
552 <p>Here I edit the file <code>file1</code> with <a href="http://localhost:8080/learn.html#editor">Sublime Text</a>, first by using the full path, and then by adding the directory to <code>PATH</code> so that Bash can find <code>subl</code>.</p>
553
554 <p>I have Microsoft Word on Windows. From the Windows Command Prompt (not Bash):</p>
555
556 <code block>
557 C:\Users\fschmidt>winword
558
559 C:\Users\fschmidt>where winword
560 C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE
561 </code>
562
563 <p><code>winword</code> runs Microsoft Word. The Command Prompt <code>where</code> command is like the Bash <code>which</code> command. So now on MSYS2:</p>
564
565 <code block>
566 ~ $ winword
567 bash: winword: command not found
568 ~ $ echo $PATH
569 /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Program Files/TortoiseHg:/c/Program Files/Java/jdk1.8.0_202/bin
570 ~ $ PATH="$PATH:/c/Program Files/Microsoft Office/root/Office16"
571 ~ $ echo $PATH
572 /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Program Files/TortoiseHg:/c/Program Files/Java/jdk1.8.0_202/bin:/c/Program Files/Microsoft Office/root/Office16
573 ~ $ winword
574 ~ $
575 </code>
576
577 <p>Returning to the Mac, there is another way to run applications found in Finder's "Applications" simply as applications instead of as commands.</p>
578
579 <code block>
580 ~/learn $ open -a 'Sublime Text' file1
581 </code>
582
583 <p>Another useful environment variable is <code>PS1</code> which controls the command prompt. I already have this set up, but if I didn't:</p>
584
585 <code block>
586 Franklins-MacBook-Pro:learn fschmidt$ echo $PS1
587 \h:\W \u\$
588 Franklins-MacBook-Pro:learn fschmidt$ PS1="\w $ "
589 ~/learn $ echo $PS1
590 \w $
591 ~/learn $
592 </code>
593
594 <p>Google "bash PS1" for more info.</p>
595
515 <% 596 <%
516 end 597 end
517 } 598 }
518 bash_profile = { 599 bash_profile = {
519 title = [[.bash_profile]] 600 title = [[.bash_profile]]
520 content = function() 601 content = function()
521 %> 602 %>
522 <p>later</p> 603 <code block>
604 ~/learn $ cd
605 ~ $ ls .bash_profile
606 .bash_profile
607 </code>
608
609 <p>If <code>.bash_profile</code> isn't found then do <code>touch .bash_profile</code> to create it. This file contains Bash commands that are run when Bash starts. If you already have this file, it is likely to contain comments that start with <code>#</code>. Comments are ignored like this:</p>
610
611 <code block>
612 ~ $ # comment line, does nothing
613 ~ $ echo whatever # end of line comment
614 whatever
615 ~ $
616 </code>
617
618 <p>To edit <code>.bash_profile</code> on a Mac, you can do:</p>
619
620 <code block>
621 ~ $ open -a 'Sublime Text' .bash_profile
622 </code>
623
624 <p>To edit <code>.bash_profile</code> on Windows, you can do:</p>
625
626 <code block>
627 ~ $ notepad .bash_profile
628 </code>
629
630 <p>Now try adding this line to <code>.bash_profile</code>:</p>
631
632 <code block>
633 echo hello there
634 </code>
635
636 <p>Now when you open a new Bash terminal, you should see "hello there". <code>.bash_profile</code> runs when Bash is started by opening a new Bash terminal.</p>
637
638 <p>I set <code>PS1</code> and <code>PATH</code> in <code>.bash_profile</code> to have the command prompt I want, and access to the commands that I want. I suggest that you make the <a href="https://www.sublimetext.com/docs/command_line.html">Sublime Text command</a> <code>subl</code> available in <code>PATH</code>.</p>
639
640 <%
641 end
642 }
643 ctrl_c = {
644 title = [[Control+c]]
645 content = function()
646 %>
647 <code block>
648 ~/learn $ sleep 3
649 ~/learn $ sleep 30
650 ^C
651 ~/learn $
652 </code>
653
654 <p><code>sleep 3</code> sleeps for 3 seconds, meaning it does nothing for 3 seconds. I waited 3 seconds for this command to finish. Then I ran <code>sleep 30</code> which would sleep for 30 seconds, but I lost my patience and pressed control+c which interrupts the program and breaks out of it. You can try control+c if you ever get stuck waiting for a command to finish.</p>
523 <% 655 <%
524 end 656 end
525 } 657 }
526 later = { 658 later = {
527 title = [[placeholder]] 659 title = [[placeholder]]